How to append data to existing file java.

How to append data to existing file java.


Posted in : Core Java Posted on : October 21, 2010 at 12:35 PM Comments : [ 0 ]

In this example, you will see how to append data in a existing file in java.

How to append data to existing file java.

In this example, you will see how to append data in a existing file in java. There is two classes FileWrite and BufferedWriter, that is used to append data in a file.  The FileWriter() class constructor simply overwrite the content of file. But if we put a boolean value true with file the name. Then the constructor append the specified data at the end of file.

Code:
AppendFileDemo.java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class AppendFileDemo {
	public static void main(String[] args) throws IOException {
	FileWriter fileWriter=new FileWriter("TestFile.txt",true);
	BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
	bufferedWriter.write("Append.");
	bufferedWriter.close();
	System.out.println("Data successfully appended.");
	}
   }
Output:
C:\Bharat>java AppendFileDemo
Data successfully appended.

Download this code.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics