aspose file tools*
The moose likes Beginning Java and the fly likes system.out.println vs. \r\n Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "system.out.println vs. \r\n" Watch "system.out.println vs. \r\n" New topic

system.out.println vs. \r\n

Elaine Grimm

Joined: Jun 13, 2010
Posts: 4
I am sorry if this has been covered somewhere else I could not find it. I am fairly new to Java programming and am wondering which is better to use:
David Newton

Joined: Sep 29, 2008
Posts: 12617

No, unless you have a specific need for \r\n.
Elaine Grimm

Joined: Jun 13, 2010
Posts: 4
OK thanks for your reply. I know in small programs ir probably doesn't make that much difference, I just thought within a larger program using \r\n may be a better way to insert lines and reduce the amount of code. As a new programmer I am trying to to keep future development and implementation in mind when creating simple programs.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42951
But you should be aware of what the difference is: println will use the "line.separator" system property, which differs between platforms. On Windows, it's "\r\n", while on Unix derivatives (like BSD, OS X and Linux) it's "\n". This may matter if you plan to use files cross-platform.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19794

System.out.println will use \r, \n or \r\n, depending on the underlying operating system. So yes, unless you really need \r\n just use println (or %n when using System.out.printf).

Now I know you're going to ask, "when do you really need \r\n?". I can think of two reasons:
1) your output files need to be read with Notepad (the worthless little program can't handle \r or \n, only \r\n)
2) you're working with sockets or HTTP requests. Don't println to a Socket applies, and I believe HTTP explicitly requires \r\n as line separators.

How To Ask Questions How To Answer Questions
Elaine Grimm

Joined: Jun 13, 2010
Posts: 4
Thank you for that information I was not aware of that!
I agree. Here's the link:
subject: system.out.println vs. \r\n