Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

'line separator' and 'newline character'?  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is 'line separator' and 'newline character'? in the class-'PrintWriter',the jdk says that,'These methods use the platform's own notion of line separator rather than the newline character',
does it mean that 'line separator' has many representations and 'newline character' means '\n'? I do not think it right if it in 'linux' or another 'OS'?
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

A newline character is always '\n' (ASCII x10).

A line separator is platform-dependent. It's "\n" on Unix/Linux/OS X/BSD, "\r\n" on Windows, "\r" on MacOS, and possibly something still different on some other obscure platform.

In Java you can check the "line.separator" system property to see what it is. When writing files, BufferedWriter.newLine() gives you a way to write that value.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:When writing files, BufferedWriter.newLine() gives you a way to write that value.



Which can be a mixed blessing. If the file you are writing to should be portable and adhere to a specification you should clearly pick a specific line seperator and use it always, instead of relying on methods such as newLine(), which will be platform specific.
 
Marshal
Posts: 58363
178
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
The char 0x000a is shown as \n but its real name is line feed. You can see the weird combinations Ulf mentioned on Wikipedia, if you have a few minutes to waste
 
k kju
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your response...
 
Campbell Ritchie
Marshal
Posts: 58363
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!