• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Creating a new file using PrintWriter.

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem with the PrintWriter class. I am trying to get it to create an new file that has more than just the last line of the existing file. I want it to print all the lines from an existing file.


[ December 12, 2007: Message edited by: Kalona Ark ]
 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kalona,
your problem is not clear... whatever lines of code you have listed above seems to be fine... so if you can mention what exactly is the problem you are having or atleast provide your source code....
 
Sheriff
Posts: 22041
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to add to the file instead of overwriting its contents, you need to use FileWriter directly, as that has the possibility to specify whether to overwrite or append:


Don't be worried about using a FileWriter - this is exactly what PrintWriter uses when created using a String or File.
 
Kalona Ark
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have 5 lines of text saved in "input.txt". I want it to have all 5 lines to show in the "output.txt".
[ December 12, 2007: Message edited by: Kalona Ark ]
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just open the file once, before the loop; and close it just once, after the loop. In other words, move the following three lines above the "while" loop:

File file = new File("output.txt");
FileWriter writer = new FileWriter(file, true);
PrintWriter output = new PrintWriter(writer);

And move this one outside the closing brace of the while loop:

output.close();

Just leave the output.println() call inside the loop.
 
Kalona Ark
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you soooo much. I was getting a major headache trying to figure this one out. I would have never thought of that.

Thanks to everyone else who have helped too.
[ December 12, 2007: Message edited by: Kalona Ark ]
 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic