• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method to Print to txt file  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Glenn Jayasuriya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!