Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i write java output in text file continuously

Tags:

java

I tried to get the output of java program as a text file.but when i print output in a text file and for the next set of input it overwrites on it.I want to see all of my output in an order in a text file.How can I?

like image 984
Jarachanthan Ratnakumar Avatar asked Aug 12 '13 18:08

Jarachanthan Ratnakumar


2 Answers

If using FileWriter , pass true as a second argument to FileWriter to turn on "append" mode.

fout = new FileWriter("filename.txt", true);

FileWriter usage reference

like image 190
Cristian Meneses Avatar answered Oct 21 '22 13:10

Cristian Meneses


Assuming you're using a FileWriter, you can provide true as the second parameter to the FileWriter(java.io.File, boolean) or FileWriter(java.lang.String, boolean)constructor, to indicate that you wish to append to the file, rather than overwriting.

It may be more efficient, however, to append everything to a StringBuilder/StringBuffer beforehand, and only write once.

like image 40
Jon Newmuis Avatar answered Oct 21 '22 13:10

Jon Newmuis