• Post Reply Bookmark Topic Watch Topic
  • New Topic

PrintWriter only prints the last line?  RSS feed

 
H Johong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm tyring to print the same output in console to a text file, but I can only get the last line of the console output in the text file, not sure what is wrong with my code:



Console output:

[time ] in input1.txt: 0.86
[darvos ] not found
[commies ] not found
[exterminate ] not found
[doctor ] in input1.txt: 0.22

Text file output:

[doctor ] in input1.txt: 0.22

What's causing only the last time to be printed in text file? Are there better ways to print console outputs into a text file than PrintWriter?
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You re-open the file each time round the loop and this overwrites the file; it does not append to it. you should open and close the output file outside the loop.
 
H Johong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!