How do I append to end of a file in Java?
Created May 4, 2012
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. raf.writeBytes( "Log restarted at 13:00pm 3-2-2000 "); raf.close(); } catch (IOException ex ) { ex.printStackTrace(); }or you can use FileWriter / FileOutputStream and open it for append:
FileWriter writer = new FileWriter("filename.txt", true);