aspose file tools
The moose likes Beginning Java and the fly likes File writing 1 line at a time Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "File writing 1 line at a time" Watch "File writing 1 line at a time" New topic
Author

File writing 1 line at a time

Barry Brashear
Ranch Hand

Joined: Jun 05, 2001
Posts: 303
How can I set up an output file and and write so each write goes on a
separate line?

Thanks.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
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

Joined: Jul 08, 2003
Posts: 24054
    
  13

This code is for JDK 1.5:



[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: File writing 1 line at a time
 
Similar Threads
File Input Stream
I am new to JSP , what is <%@ taglib ?
VBScript to compare files... not really working
csv file
adding a newline character to a file using fileoutputstream