Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

\n and \r seem to work everywhere. Why is line.separator more portable?

I was just perusing through questions, and I found System.getProperty(line.separator) used in place of \n with the author's comment that the code was "portable". Reading through various forums, I've seen two groups:

  1. People who say there's a difference between Linux's and Windows' interpretation of newline characters, and this compensates for that (with no clear evidence).
  2. People who say there's no difference by showing code and output examples, which obviously only applies to that code example and not universally.

My feeling is: it's probably non-standard OS's, like for example your company's industrial scanner's OS for example, where you'll notice a difference. When am I going to see a difference between \n and line.separator? Can you show an example, please? How did you go about discovering where the variation occurs?

like image 690
Wolfpack'08 Avatar asked May 17 '14 02:05

Wolfpack'08


People also ask

Is the line separator the same on all platforms What is the line separator on Windows?

The line separator varies from computer to computer, from Operating System to Operating System. You will get different line separators on Windows and Unix. If you run this: System. getProperty("line.

Which line separator should I use?

Line separators are also very important in certain network protocols. It's best to respect the standards. Just use \n . When Windows users tell you that your program doesn't work, just tell them it doesn't support Windows or pre-OSX Mac and they need to switch to a different system.

What is the difference between \n and \r in Java?

\n is a line feed (LF) character, character code 10. \r is a carriage return (CR) character, character code 13. What they do differs from system to system. On Windows, for instance, lines in text files are terminated using CR followed immediately by LF (e.g., CRLF).

What is a line separator?

The line separator used by the in-memory representation of file contents is always the newline character. When a file is being loaded, the line separator used in the file on disk is stored in a per-buffer property, and all line-endings are converted to newline characters for the in-memory representation.


3 Answers

Here's the standard line separators by OS:

Windows: '\r\n'
Mac (OS 9-): '\r'
Mac (OS 10+): '\n'
Unix/Linux: '\n'

What that means is that if you hard code your line separators as \n, you're going to get the expected results in Linux and OS X, but Windows won't recognize the line endings properly. However, by using the more general line.separator to represent your line endings, they'll resolve to whatever the executing operating system's expected implementation of line endings may happen to be.

like image 171
Sam Hanley Avatar answered Sep 21 '22 04:09

Sam Hanley


Incorrect line endings are a frequent annoyance. For example, this is what Windows Notepad shows when writing a file with \n instead of \r\n (Windows' line.separator):

Notepad with boxes instead of line breaks

Those little boxes were supposed to be line breaks.

The other way around, when \r\n is used in place of \n (Unix' line.separator), is way worse, and breaks shell scripts and config files in weird and wonderful ways.

For example, this is what sh outputs on Debian and derived distros when trying to run a shell script that just contains ls but with \r\n line separators (it looks trashed because the carriage return causes the terminal to overwrite parts of the line):

: not foundsh: ls

There are several questions per day on StackOverflow from people being bitten by this, such as here, here and here.

like image 45
that other guy Avatar answered Sep 23 '22 04:09

that other guy


If you get this wrong, you WILL mess up your ability to exchange data with other applications. You need to understand when you can get away with assuming that Java will help you... and when you can't.

Linux applications normally use only the linefeed character (\n) as its line break. Windows applications use the combination of carriage return followed by line feed (\n\r).

If you're using high-level Java I/O routines -- if you're going through Readers and Writers which process characters, rather than low-level Streams and especially byte streams -- the libraries will translate \n (which java, by convention, uses internally as its newline) into the platform-appropriate representation. If you're using lower-level functions, you have to be aware of that distinction and Do The Right Thing yourself.

like image 43
keshlam Avatar answered Sep 19 '22 04:09

keshlam