Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > which char should I write to a file to make a new line?

Reply
Thread Tools

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

 
 
jtl.zheng
Guest
Posts: n/a
 
      07-21-2006
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
^_^

 
Reply With Quote
 
 
 
 
bryanlabutta@gmail.com
Guest
Posts: n/a
 
      07-21-2006
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!

jtl.zheng wrote:
> 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
> ^_^


 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      07-21-2006
"jtl.zheng" <> wrote in message
news: ups.com...
> 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' ??


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/ap...getProperties()


- Oliver

 
Reply With Quote
 
Tajonis
Guest
Posts: n/a
 
      07-21-2006
> 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/ap...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.

 
Reply With Quote
 
Steve W. Jackson
Guest
Posts: n/a
 
      07-21-2006
In article <5I5wg.115456$A8.89977@clgrps12>,
"Oliver Wong" <> wrote:

> "jtl.zheng" <> wrote in message
> news: ups.com...
> > 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' ??

>
> 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/ap...getProperties()
>
>
> - Oliver


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

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
 
Reply With Quote
 
jtl.zheng
Guest
Posts: n/a
 
      07-21-2006
haha
Thank you very much

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

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      07-22-2006
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.

 
Reply With Quote
 
jtl.zheng
Guest
Posts: n/a
 
      07-22-2006
yes...so I am using System.getProperty("line.separator") now
thanks
: )

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57