[Logo]
Forums Register Login
Method to Print to txt file
Hi I'm trying to have a method that takes an input string and print it out to a txt file.
This is my attempt:


The issue is that it's only printing out the last instance this method is called to the file out.txt.
So my out.txt has only one line printed when I called it multiple times to print.
(1 like)
You are telling the PrintWriter to overwrite the existing data in the file, you need to use append if you open an existing file to preserve the contents and add your new data.
Glenn Jayasuriya wrote:Hi I'm trying to have a method that takes an input string and print it out to a txt file.
This is my attempt:


The issue is that it's only printing out the last instance this method is called to the file out.txt.
So my out.txt has only one line printed when I called it multiple times to print.
 
Thanks I just changed "new FileWriter("output.txt")" to "new FileWriter("out.txt", true) and it worked perfectly.

Les Morgan wrote:You are telling the PrintWriter to overwrite the existing data in the file, you need to use append if you open an existing file to preserve the contents and add your new data.
Glenn Jayasuriya wrote:Hi I'm trying to have a method that takes an input string and print it out to a txt file.
This is my attempt:


The issue is that it's only printing out the last instance this method is called to the file out.txt.
So my out.txt has only one line printed when I called it multiple times to print.
 
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Nov 21, 2017 14:11:01.