What should I see when I use the following?
System.out.println("LineSeperator1: "+System.getProperty("line.separator"));
System.out.println("LineSeperator2: "+System.lineSeparator());
I get the following back:
LineSeperator1:
LineSeperator2:
Is it empty? invisible? shouldn there be something like \r or \n
?
I use windows 7, eclipse and jdk 1.8.
As you expect, the line separator contains special characters such as
'\r'
or'\n'
that denote the end of a line. However, although they would be written in Java source code with those backslash escape sequences, they do not appear that way when printed. The purpose of a line separator is to separate lines, so, for example, the output ofis
rather than, say
You can even see this in the output of your code: the output of
had an extra blank line before the output of the next statement, because there was a line separator from
System.getProperty("line.separator")
and another from the use ofprintln
.If you really want to see what the escaped versions of the line separators look like, you can use
escapeJava
from Apache Commons. For example:On my system, this outputs
Note that I had to run it in the same folder as the .jar file from the Apache download, compiling and running with these commands
Printing something afterwards will show the effect:
Gives