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

I was just looking through the questions, and I found System.getProperty(line.separator) instead of \n with the author commenting that the code was "portable". Looking through various forums, I saw two groups:

  • People who say that there is a difference between interpreting newline characters between Linux and Windows, and this compensates for this (without any clear evidence).
  • People who say that there are no differences by demonstrating code and output examples that are obviously applicable only to this code example, and not everywhere.

My feeling: this is probably non-standard OS, for example, for example, your company industrial OS scanner, where you will notice the difference. When will I see the difference between \n and line.separator ? Can you set an example please? How did you find out where the change is?

+8
java portability newline linefeed
source share
3 answers

Wrong line endings are often annoying. 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

These little boxes were supposed to be line breaks.

Another way, when \r\n used instead of \n (Unix 'line.separator), is much worse and breaks shell scripts and configuration files in strange and wonderful ways.

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

 : not foundsh: ls 

There are several questions from StackOverflow from people bitten by this, for example here , here and here .

+8
source share

Here are the standard OS line separators:

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

This means that if you hardcode the line breaks as \n , you will get the expected results on Linux and OS X, but Windows will not correctly recognize the final lines. However, using a more general line.separator to represent the endings of your line, they will solve everything that the operating system expects, and execution of line execution may turn out to be.

+10
source share

If you make a mistake, you will ruin your ability to exchange data with other applications. You need to understand when you can get away with the assumption that Java will help you ... and when you cannot.

Linux applications typically use only the line feed character (\ n) as a line break. Windows applications use a carriage return combination followed by a string (\ n \ r).

If you use high-level Java I / O routines - if you look at Readers and Writers that process characters rather than low-level streams and especially byte streams - the libraries will translate \ n (which java, by convention, uses internally as its new string) in the appropriate platform view. If you use lower-level functions, you should be aware of this difference and do the “Right Thing” yourself.

+6
source share

All Articles