Important alert: (current site time 7/15/2013 3:24:16 PM EDT)
 

VB icon

Append data to a file

Email
Submitted on: 6/22/1998
By:  
Level: Not Given
User Rating: By 6 Users
Compatibility: Java (JDK 1.1)
Views: 38565
 
     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: 
//**************************************

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 21 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
1/24/2002 9:45:22 AMRafi

Dear sir,

Can you please put some sense in this for me?
I wold like to learn how to read write and append in Java.
I found this on the net but I can’t put it together.
I like to have an html that gets 3 parameters name, address and file name. Then it will write it to the file or read it from the file.

Like:

Rafi, 23 Pico BLV, test.txt >> will append “rafi” to the file “test.txt”

And

Rafi, test >> will look for record with name rafi and return “ 23 Pico BLV’ as entered before.


Can you help?
Regards,

RA
(If this comment was disrespectful, please report it.)

 
7/25/2002 1:26:38 PMAmir Tasleem

respected sir
i need ur assistance related to files handling in JSP.
it would be really so nice of u if u send me some use full information related to the topic.
(If this comment was disrespectful, please report it.)

 
12/3/2002 5:28:39 AM

I'm a beginner and I'm trying to write a daytimer program to keep names tel#'s bdays etc and also utilizes the sytem clock to implement a date reminder and I have no idea how to do read write together with the implementation of the system clock to check the data in the files. Any help would be very appreciated.
(If this comment was disrespectful, please report it.)

 

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.