Home Java Example Java Io Append To File - Java Tutorial
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Append To File - Java Tutorial

Advertisement
In the section of Java Tutorial you will learn how to write java program to append a file.

Append To File - Java Tutorial

     

introduction

In the section, you will learn how the data is appended to an existing file. We will use the class FileWriter and BufferedWriter to append the data to a file.

 FileWriter
The FileWriter is a class used for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. This constructor simply overwrite the contents in the file by the specified string but if you put the boolean value as true with the file name (argument of the constructor) then the constructor append the specified data to the file i.e. the pre-exist data in a file is not overwritten and the new data is appended after the pre-exist data.

BufferedWriter

The BufferWriter class is used to write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

Here is the code of java program to write text to a file:

import java.io.*;
class FileWrite 
{
 public static void main(String args[])
  {
  try{
  // Create file 
  FileWriter fstream = new FileWriter("out.txt",true);
  BufferedWriter out = new BufferedWriter(fstream);
  out.write("Hello Java");
  //Close the output stream
  out.close();
  }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

Download the code

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2007

Ask Questions?    Discuss: Append To File - Java Tutorial   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
raviteja
August 31, 2011
Thanks for the code

Thanks
anshul
June 6, 2013
thanks

thanks dear..it really works.....one single true do the magic.
eashwary
February 25, 2012
to open file in append mode and write a object

how open file in append mode and write a object
Sachid
May 2, 2012
Thanks!

Thanks, it was useful for me :)
diva
June 5, 2012
i love u

i realy luv u
Vivek Mathews
June 6, 2012
Java

write a java program which open an existing file and append text file?
Vivek Mathews
June 6, 2012
Java

write a java program which open an existing file and append text file
priya
July 14, 2013
javacode

this code is useful for me.. thanks u..
DMCA.com