A friendly place for programming greenhorns!
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
Author
File writing 1 line at a time
Barry Brashear
Ranch Hand
Joined: Jun 05, 2001
Posts: 303
posted
Dec 08, 2005 08:37:00
0
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
posted
Dec 08, 2005 08:42:00
0
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
I like...
posted
Dec 08, 2005 08:43:00
0
This code is for JDK 1.5:
String[] lines = ... PrintWriter pw = new PrintWriter(filename); for (String line: lines) pw.println(line); pw.close();
[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
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter