• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

system.out.println vs. \r\n

 
Elaine Grimm
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, unless you have a specific need for \r\n.
 
Elaine Grimm
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Elaine Grimm
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for that information I was not aware of that!
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic