Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
jtl.zheng - 21 Jul 2006 15:09 GMT
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
^_^
bryanlabutta@gmail.com - 21 Jul 2006 15:28 GMT
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!

> I want to write a String type to a local file
> the code like:
[quoted text clipped - 21 lines]
> thank you
> ^_^
Oliver Wong - 21 Jul 2006 15:41 GMT
> I want to make a new line in the dest.txt
> just like:
[quoted text clipped - 4 lines]
> what should I add to the String src
> a char '\n'  ??

   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()

   - Oliver
Tajonis - 21 Jul 2006 16:04 GMT
>     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.
Steve W. Jackson - 21 Jul 2006 16:18 GMT
> > I want to make a new line in the dest.txt
> > just like:
[quoted text clipped - 10 lines]
>
>     - Oliver

And I have to wonder why the OP doesn't use a PrintWriter instead, so
that this is a non-issue.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

jtl.zheng - 21 Jul 2006 17:13 GMT
haha
Thank you very much

the "\r\n" and  System.getProperty("line.separator") are both OK
^_^
Andrew Thompson - 22 Jul 2006 01:25 GMT
...
> 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.
jtl.zheng - 22 Jul 2006 02:37 GMT
yes...so I am using System.getProperty("line.separator") now
thanks
: )


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.