Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is There Any Difference Between .....

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any difference between
System.getProperty("line.separator"); and
/n
?
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any difference between
System.getProperty("line.separator"); and
/n

I think you mean "\n" right?
System.getProperty("line.separator") is more portable because it will return the appropriate new line character for the os that is running the java program.
As an example, BufferedReader.newLine() uses System.getProperty("line.separator") to determine how to write a new line.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. If you use an OS other than Unix, it's often different. On Windows, line.separator will be "\r\n"; on a Mac, I think it's "\n\r" (or something like that). It's often a good idea to look up the line.separator property in order to use the separator that's appropriate to the system you're on. Or use the println() method in PrintStream or PrintWriter to provide it automatically. However if you're working with an application that communicates across different platforms (e.g. a Unix-based server that provides files that will be used on Windows or Mac machines) then you may need to use a separator which is not the system default. Just something to be aware of.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic