Java change system new-line character

Andreas picture Andreas · Mar 27, 2014 · Viewed 9.2k times · Source

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?

Answer

Holger picture Holger · Mar 27, 2014

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.