Search code examples
javafilefwrite

Writing from console to text file, file data not saved


I am trying to save the content of the java console into a text file but each time I close the program the text file goes blank and rewrites to it. i.e. if I write to a file today, close the program and come back and run it again tomorrow, it has remembered the information written to it.


Solution

  • You want to open the OutputStream in append mode. Demo code:

    PrintWriter out = new PrintWriter(
                         new FileOutputStream(new File(filename), true)); 
    

    What you experience is the normal behavior when you write a stream to a file, and this is not specific to the Java API.