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

Discussion in 'Java' started by jtl.zheng, Jul 21, 2006.

  1. jtl.zheng

    jtl.zheng Guest

    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
    ^_^
    jtl.zheng, Jul 21, 2006
    #1
    1. Advertising

  2. jtl.zheng

    Guest

    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
    > ^_^
    , Jul 21, 2006
    #2
    1. Advertising

  3. jtl.zheng

    Oliver Wong Guest

    "jtl.zheng" <> wrote in message
    news:...
    > 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/api/java/lang/System.html#getProperties()


    - Oliver
    Oliver Wong, Jul 21, 2006
    #3
  4. jtl.zheng

    Tajonis Guest

    > 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.
    Tajonis, Jul 21, 2006
    #4
  5. In article <5I5wg.115456$A8.89977@clgrps12>,
    "Oliver Wong" <> wrote:

    > "jtl.zheng" <> wrote in message
    > news:...
    > > 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/api/java/lang/System.html#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
    Steve W. Jackson, Jul 21, 2006
    #5
  6. jtl.zheng

    jtl.zheng Guest

    haha
    Thank you very much

    the "\r\n" and System.getProperty("line.separator") are both OK
    ^_^
    jtl.zheng, Jul 21, 2006
    #6
  7. 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.
    Andrew Thompson, Jul 22, 2006
    #7
  8. jtl.zheng

    jtl.zheng Guest

    yes...so I am using System.getProperty("line.separator") now
    thanks
    : )
    jtl.zheng, Jul 22, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    549
  2. wwj
    Replies:
    24
    Views:
    2,507
    Mike Wahler
    Nov 7, 2003
  3. John Devereux

    Should I use "char" or "unsigned char" for strings?

    John Devereux, Mar 28, 2005, in forum: C Programming
    Replies:
    4
    Views:
    635
    Eric Sosman
    Mar 29, 2005
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,043
    Ian Collins
    May 9, 2006
  5. scad
    Replies:
    23
    Views:
    1,162
    Alf P. Steinbach
    May 17, 2009
Loading...

Share This Page