Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File writing 1 line at a time

 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I set up an output file and and write so each write goes on a
separate line?

Thanks.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create an external text file with

FileWriter writer = new FileWriter(fileName);

where fileName is the name of the file you want to create.

Be careful. With this constructor of FileWriter, if the file already exists, it will be overwritten.

Then you can wrap it in a PrintWriter.

PrintWriter output = new PrintWriter(writer);

and then to write a line to the file and include a newline in the output, you would use

output.println(...);

Make sure that you close the PrintWriter so that any text buffered in memory is written to the file.

output.close();
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is for JDK 1.5:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic