• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Method to Print to txt file

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 975
23
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Loren Agular
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic