VB icon

Append data to a file

Email
Submitted on: 1/1/2015 4:18:00 AM
By: Anonymous (from psc cd)  
Level: Not Given
User Rating: By 6 Users
Compatibility: Java (JDK 1.1)
Views: 2751
 
     In Java 1.1 you can just pass true as the second argument to this FileOutputStream constructor to indicate that you want to append data to the file: public FileOutputStream(String name, boolean append) throws IOException In Java 1.0, however, you must use the java.io.RandomAccessFile class that lets you read and write bytes from arbitrary locations in a file. This class implements DataInput and DataOutput so you have all the methods of DataInputStream and DataOutputStream available to you. To create a new random access file pass the name of the file and the mode to the constructor. The mode is either "r" (read-only) or "rw" (read and write). The length() method returns a long that tells you how many bytes there are in a file and the seek(long p) method lets you position the file pointer at a particular point in the file. Thus to start writing at the end of a RandomAccessFile raf, you first raf.seek(raf.length()). The following example demonstrates by appending the string "Kilroy was here!" to every file specified on the command line. (Java FAQ:found on the web at:http://sunsite.unc.edu/javafaq/javafaq.html)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Append data to a file
// Description:In Java 1.1 you can just pass true as the second argument to this FileOutputStream constructor to indicate that you want to append data to the file:
public FileOutputStream(String name, boolean append) 
 throws IOException
In Java 1.0, however, you must use the java.io.RandomAccessFile class that lets you read and write bytes from arbitrary locations in a file. This class implements DataInput and DataOutput so you have all the methods of DataInputStream and DataOutputStream available to you.
To create a new random access file pass the name of the file and the mode to the constructor. The mode is either "r" (read-only) or "rw" (read and write). The length() method returns a long that tells you how many bytes there are in a file and the seek(long p) method lets you position the file pointer at a particular point in the file. Thus to start writing at the end of a RandomAccessFile raf, you first raf.seek(raf.length()). The following example demonstrates by appending the string "Kilroy was here!" to every file specified on the command line.
(Java FAQ:found on the web at:http://sunsite.unc.edu/javafaq/javafaq.html)
// By: Anonymous (from psc cd)
//**************************************

import java.io.*;
class AppendToAFile {
 public static void main (String args[]) {
for (int i = 0; i < args.length; i++) {
 //First open the file you want to append to
 try {
RandomAccessFile raf = new RandomAccessFile(args[i], "rw");
// Position yourself at the end of the file
raf.seek(raf.length());
// Write the String into the file. Note that you must
// explicitly handle line breaks.
raf.writeBytes("\nKilroy was here!\n");
 }
 catch (IOException e) {
System.out.println("Error opening file: " + e);
 }
 
}
 } 
}


Other 17 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.