Java I/O How to - Append or add some strings to the file








Question

We would like to know how to append or add some strings to the file.

Answer

   /*from   w  w w.  j av  a 2s. c o  m*/
 
import java.io.File;
import java.io.FileWriter;

public class Main {
  public static void main(String[] args) throws Exception{
    File file = new File("user.txt");

    FileWriter writer = new FileWriter(file, true);
    writer.write("username=java;password=secret" + System.getProperty("line.separator"));
    writer.flush();
    writer.close();
  }
}