which char should I write to a file to make a new line?

J

jtl.zheng

I want to write a String type to a local file
the code like:

------------------------------
String src="xxxxxxxxxxxxxxxxxx";
BufferedWriter out = new BufferedWriter(new FileWriter("dest.txt"));
out.write(src);
out.close();
------------------------------

and I want to make a new line in the dest.txt
just like:

"xxxxxxxxx
yyyyyyyyy" (next line)

what should I add to the String src
a char '\n' ??
but when I add this char '\n' to the String src I can't see the new
line when open the dest.txt file in NOTEPAD.EXE
it is just like:
"xxxxxxxxxyyyyyyyyy"

thank you
^_^
 
B

bryanlabutta

Try adding '\r\n' instead of just '\n' The '\n' specifies a new line
but you need to specify a carriage return by putting '\r' first. That
should make the line split into two as you desire.

Good luck!
 
T

Tajonis

The line seperation String (not nescessarily a single character) is
platform dependent. You can query it using System.getProperties:
http://java.sun.com/j2se/1.3/docs/api/java/lang/System.html#getProperties()

to elaborate and to make it even easier you could use this

String str = "xxxxxxxxx" + System.getProperty("line.separator");

this should provide the ability to determine which line terminator to
use for each platform that your application runs on.
 
J

jtl.zheng

haha
Thank you very much

the "\r\n" and System.getProperty("line.separator") are both OK
^_^
 
A

Andrew Thompson

jtl.zheng wrote:
....
the "\r\n" and System.getProperty("line.separator") are both OK

But while "\n\r" is fragile, System.getProperty("line.separator")
should work X-plat, now and in the future.

Andrew T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top