Use
pw.println(data);
instead of
pw.write(data);
Main Topics
Browse All TopicsI want read a text file and output to another text file
This is my code
FileInputStream s = new FileInputStream(fileName);
DataInputStream dis = new DataInputStream(s);
InputStreamReader r = new InputStreamReader(s);
FileOutputStream fos = new FileOutputStream(output_fi
PrintWriter pw = new PrintWriter(fos, true);
data = reader.readLine();
while (data!=null)
{
pw.write(data);
data = reader.readLine();
}
my problem now is that when I open the new output file, there will be just one line without any new line ...
so, instead of using pw.write(data+"\n") for every new line, can I use another code?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
what I actually want to do is to manipulate the string first before it is put on a new output file.
data = reader.readLine();
while (data!=null)
{
//break the data into characters including the new line...
//then write the result into the output file
// pw.write(data);
data = reader.readLine();
}
what I find in readLine() is that it only read string, but exclude the new line (\n) character.
so can I actually modify the "data = reader.readLine();" part with something else that can include the new line character?
> what I find in readLine() is that it only read string, but exclude the new line (\n) character.
Thats the expected behavior - api says so.
so can I actually modify the "data = reader.readLine();" part with something else that can include the new line character?
> Use data = data + "\n";
> If you *only* want to use new line , i suggest to use println method
I would suggest using the line separator property
String NEWLINE = System.getProperty("line.s
data = reader.readLine();
while (data!=null)
{
//break the data into characters including the new line...
//then write the result into the output file
// pw.write(data+ NEWLINE);
data = reader.readLine();
}
it seems like you want on one hand to process the text without the EOL character and on the other hand to output the text including the EOL character without any additional "work"
Business Accounts
Answer for Membership
by: clim1219Posted on 2007-03-20 at 22:31:52ID: 18762060
Hi goldragon,
Instead of using pw.write(data+"\n") you can use pw.println( data ); like:
while (data!=null) {
pw.println( data );
data = reader.readLine();
}
an alternative would be to use strictly streams without the writer like:
InputStream inputStream = new FileInputStream( "test.txt" );
FileOutputStream outputStream = new FileOutputStream( "test_output.txt" );
int available = inputStream.available();
while( available != 0 ) {
outputStream.write( inputStream.read() );
available = inputStream.available();
}
this way the \n is read as is and written to the output stream properly.
Cheers!
Chris