How do I append to end of a file in Java?

Nicholas Wright

You can use java.io.RandomAccessFile and something like the following:

try {
  RandomAccessFile raf = 
    new RandomAccessFile("filename.txt", "rw");
  raf.skipBytes( (int)raf.length() );
  // You are now at the end of the file,
  // and can start writing new data out, e.g.
    "Log restarted at 13:00pm 3-2-2000
} catch (IOException ex ) {
or you can use FileWriter / FileOutputStream and open it for append:

FileWriter writer = 
  new FileWriter("filename.txt", true);
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.