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:
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?
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.
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.
\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).
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.
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.
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):
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With