问题
On Windows, using System.out.println()
prints out \n\r
while on a Unix system you would get \n
.
Is there any way to tell java what new-line characters you want to use?
回答1:
Yes, there is a way and I've just tried it.
There is a system property line.separator
. You can set it using System.setProperty("line.separator", whatever)
To be sure that it indeed causes JVM to use other separator I implemented the following exercise:
PrintWriter writer = new PrintWriter(new FileWriter("c:/temp/mytest.txt"));
writer.println("hello");
writer.println("world");
writer.close();
I am running on windows now, so the result was 14 bytes long file:
03/27/2014 10:13 AM 14 mytest.txt
1 File(s) 14 bytes
0 Dir(s) 409,157,980,160 bytes free
However when I added the following line to the beginning of my code:
System.setProperty("line.separator", "\n");
I got 14 bytes long file:
03/27/2014 10:13 AM 14 mytest.txt 1 File(s) 14 bytes 0 Dir(s) 409,157,980,160 bytes free
I opened this file with notepad that does not recognize single \n
as a new line and saw one-line text helloworld
instead of 2 separate lines. So, this works.
回答2:
As already stated by others, the system property line.separator
contains the actual line separator. Strangely, the other answers missed the simple conclusion: you can override that separator by changing that system property at startup time.
E.g. if you run your program with the option -Dline.separator=X
at the command line you will get the funny behavior of System.out.println(…);
ending the line with an X
.
The tricky part is how to specify characters like \n
or \r
at the command line. But that’s system/environment specific and not a Java question anymore.
回答3:
You may try with:
String str = "\n\r";
System.out.print("yourString"+str);
but you can instead use this:-
System.getProperty("line.separator");
to get the line seperator
Returns the system-dependent line separator string. It always returns the same value - the initial value of the system property line.separator.
On UNIX systems, it returns "\n"; on Microsoft Windows systems it returns "\r\n".
回答4:
As stated in the Java SE tutorial:
To modify the existing set of system properties, use System.setProperties. This method takes a Properties object that has been initialized to contain the properties to be set. This method replaces the entire set of system properties with the new set represented by the Properties object.
Warning: Changing system properties is potentially dangerous and should be done with discretion. Many system properties are not reread after start-up and are there for informational purposes. Changing some properties may have unexpected side-effects.
In the case of System.out.println()
, the line separator that existed on system startup will be used. This is probably because System.lineSeparator() is used to terminate the line. From the documentation:
Returns the system-dependent line separator string. It always returns the same value - the initial value of the system property line.separator.
On UNIX systems, it returns "\n"; on Microsoft Windows systems it returns "\r\n".
As Holger pointed out, you need to overwrite this property at startup of the JVM.
回答5:
The method System.lineSeparator()
returns the line separator used by the system. From the documentation it specifies that it uses the system property line.separator.
来源:https://stackoverflow.com/questions/22681534/java-change-system-new-line-character