\r vs \n\r and related issues

Discussion in 'Java' started by Aaron Fude, May 30, 2004.

  1. Aaron Fude

    Aaron Fude Guest

    Hi,

    I'm a little confused by the \r \n\r differences between operating systems.
    For example, does the output of println() depend on the operating system?
    Also, what is a reliable way to use a StringTokenizer to break up input into
    lines - what do you use as a delimiter?

    Thanks,

    Aaron
    Aaron Fude, May 30, 2004
    #1
    1. Advertising

  2. Aaron Fude

    Sudsy Guest

    Aaron Fude wrote:
    > Hi,
    >
    > I'm a little confused by the \r \n\r differences between operating systems.
    > For example, does the output of println() depend on the operating system?
    > Also, what is a reliable way to use a StringTokenizer to break up input into
    > lines - what do you use as a delimiter?


    From the javadocs (always a good place to start) for PrintWriter:

    "The println() methods use the platform's own notion of line separator
    rather than the newline character."

    As to parsing input, that's why we have BufferedReader. From the
    javadocs (again) we find the following text in the readLine method
    description:

    "A line is considered to be terminated by any one of a line feed ('\n'),
    a carriage return ('\r'), or a carriage return followed immediately by
    a linefeed."

    IOW, you don't have to worry about it! I can even FTP a file from M$
    to a *NIX box and read it with no problems.
    Sudsy, May 30, 2004
    #2
    1. Advertising

  3. Aaron Fude

    Frank Guest

    > I'm a little confused by the \r \n\r differences between operating
    > systems.
    > For example, does the output of println() depend on the operating system?


    The answer here is technically yes. println() calls will write the current
    platform's default line seperator to the output. Conceptually, this should
    be relatively transparant to you though, as, regardless of the platform,
    it will generate a new line. This does get a bit problematic though, when
    the output is not necessarily displayed on the same machine that is
    generating it :/

    > Also, what is a reliable way to use a StringTokenizer to break up input
    > into
    > lines - what do you use as a delimiter?


    "\r\n" should work just fine as a delim for StringTokenizer. That way,
    either single char will mark an EOL, and a doubled instance should get
    ignored.

    Hope this helps!

    Frank
    Frank, May 30, 2004
    #3
  4. Aaron Fude

    Roedy Green Guest

    Roedy Green, May 30, 2004
    #4
  5. Aaron Fude

    Tony Morris Guest

    "Aaron Fude" <> wrote in message
    news:xgduc.16905$4A6.8274@attbi_s52...
    > Hi,
    >
    > I'm a little confused by the \r \n\r differences between operating

    systems.
    > For example, does the output of println() depend on the operating system?
    > Also, what is a reliable way to use a StringTokenizer to break up input

    into
    > lines - what do you use as a delimiter?
    >
    > Thanks,
    >
    > Aaron
    >


    http://www.xdweb.net/~dibblego/java/faq/answers.html#q44

    --
    Tony Morris
    (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
    Software Engineer
    IBM Australia - Tivoli Security Software
    (2003 VTR1000F)
    Tony Morris, May 30, 2004
    #5
  6. Aaron Fude

    Tony Morris Guest

    > I was wonderring at the time why
    > you do not mention ..
    > System.getProperty("line.separator")
    > .. in that entry.


    I'm not sure if either a) I do not understand your question or b) you
    overlooked that I did in fact mention the "line.separator" system property:
    <quote>
    You can obtain the EOL terminator by accessing the System property called
    "line.separator".
    </quote>
    Am I missing something in your question?

    The sharp eye will have noticed that I quoted the hexadecimal values for \r
    and \n incorrectly - this is pending on my "to do list" to fix.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
    Software Engineer
    IBM Australia - Tivoli Security Software
    Home : +61 7 5502 7987
    Work : +61 7 5552 4076
    Mobile : 0408 711 099
    (2003 VTR1000F)
    Tony Morris, Jun 3, 2004
    #6
  7. On Thu, 03 Jun 2004 10:42:25 GMT, Tony Morris wrote:

    > Am I missing something in your question?


    No. I was missing something in your answer. ;-)

    > The sharp eye will have noticed that I quoted the hexadecimal values for \r
    > and \n incorrectly - this is pending on my "to do list" to fix.


    This 'sharp eye' did not even
    pick up the reference to
    System.getProperty("line.separator")
    ...and your talkin' hex??

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Jun 3, 2004
    #7
  8. Aaron Fude

    Peter Kirk Guest

    "Tony Morris" <> skrev i en meddelelse
    news:Rheuc.17548$...
    >
    > "Aaron Fude" <> wrote in message
    > news:xgduc.16905$4A6.8274@attbi_s52...
    > > Hi,
    > > I'm a little confused by the \r \n\r differences between operating

    > systems.
    >
    > http://www.xdweb.net/~dibblego/java/faq/answers.html#q44


    This webpage says this:

    "\r\n" (0x00D0 0x00A0);

    Are these values ASCII, and if so are they correct?

    Peter
    Peter Kirk, Jun 4, 2004
    #8
  9. Andrew Thompson, Jun 4, 2004
    #9
  10. Aaron Fude

    Roedy Green Guest

    On Fri, 04 Jun 2004 09:39:42 GMT, Andrew Thompson
    <> wrote or quoted :

    >> "\r\n" (0x00D0 0x00A0);
    >>
    >> Are these values ASCII, and if so are they correct?

    no. A0 is often used an non-breaking space. that should read 0x0a.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 4, 2004
    #10
  11. Aaron Fude

    Peter Kirk Guest

    "Roedy Green" <> skrev i en meddelelse
    news:...
    > On Fri, 04 Jun 2004 09:39:42 GMT, Andrew Thompson
    > <> wrote or quoted :
    >
    > >> "\r\n" (0x00D0 0x00A0);
    > >>
    > >> Are these values ASCII, and if so are they correct?

    > no. A0 is often used an non-breaking space. that should read 0x0a.


    Yes, I did not have sharp enough eyes to see that it was already identified
    elsewhere under this thread as a mistake on the website.
    Peter Kirk, Jun 4, 2004
    #11
  12. Sudsy () wrote:

    : As to parsing input, that's why we have BufferedReader. From the
    : javadocs (again) we find the following text in the readLine method
    : description:

    : "A line is considered to be terminated by any one of a line feed ('\n'),
    : a carriage return ('\r'), or a carriage return followed immediately by
    : a linefeed."

    Wow, that sounds ugly. Typically a file uses one or the other or both
    together, but not normally both individually. A file that uses \n as the
    separator might have \r's and in that case they normally aren't
    separators, and vice versa for files using \r's to end the lines, any \n's
    are not normally line separators.

    However, I suppose it is an easily consistent way to handle the situation.
    Malcolm Dew-Jones, Jun 5, 2004
    #12
  13. Aaron Fude

    Tony Morris Guest

    > > >> "\r\n" (0x00D0 0x00A0);
    > > >>
    > > >> Are these values ASCII, and if so are they correct?

    > > no. A0 is often used an non-breaking space. that should read 0x0a.

    >
    > Yes, I did not have sharp enough eyes to see that it was already

    identified
    > elsewhere under this thread as a mistake on the website.
    >


    Apologies for the confusion.
    I will fix it first thing when I get back to work next week.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
    Software Engineer
    IBM Australia - Tivoli Security Software
    (2003 VTR1000F)
    Tony Morris, Jun 5, 2004
    #13
    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. Youngest Lucifer

    Platform related issues.

    Youngest Lucifer, May 5, 2006, in forum: Java
    Replies:
    2
    Views:
    316
    Youngest Lucifer
    May 8, 2006
  2. Maxwell Hammer
    Replies:
    7
    Views:
    631
    Peter Hansen
    Jun 18, 2005
  3. john
    Replies:
    19
    Views:
    500
    John Ersatznom
    Jan 6, 2007
  4. Ramon F Herrera
    Replies:
    3
    Views:
    360
    Salt_Peter
    Jun 13, 2008
  5. Michele Dondi
    Replies:
    6
    Views:
    129
    Michele Dondi
    Jul 4, 2004
Loading...

Share This Page