rwallace
rwallace

Reputation: 33365

System.out to Unix line endings on Windows

In Java, newly created output streams look at the system line separator property to decide between Unix and Windows line endings, but System.out has already been created by the time your program starts, so altering that property has no effect on that stream.

If you want System.out to use Unix line endings, despite running on Windows, is there a way to do this other than eschewing println() and always using print("...\n")?

Upvotes: 1

Views: 369

Answers (1)

Thomas Jungblut
Thomas Jungblut

Reputation: 20969

You can manually set System.out. I sometimes use it to redirect prints to a file.

For example:

System.setOutput(new PrintStream("whatever_file.txt"));

In a similar sense, you can change the PrintStream to take your line endings into account.

Alternatively you can directly set the property of the line ending character:

System.setProperty("line.separator", "\n");

This could also be done when creating the JVM by the usual property setter:

java -jar bla.jar -Dline.seperator='\n'

Upvotes: 3

Related Questions