If I write "println("string")" the code writes "string" and then \n if it's run in Windows, \r\n if it's in Linux.

Is there a way to change the behaviour of the "newline" according to my will? I tried to search some other String method, but couldn't find any that could adapt to this problem.

Obviously the final solution would be "print("String\r\n")" if I want the newline to be Windows-compatible, but it's the last thing I'd want to do.

Thanks for the suggestion

有帮助吗?

解决方案

A PrintStream uses a BufferedWriter to write, which in turn uses this line separator:

/**
 * Line separator string.  This is the value of the line.separator
 * property at the moment that the stream was created.
 */
private String lineSeparator;

You can thus use System.setProperty("line.separator", "\r\n") to set the default line separator, but it will only affect newly created PrintStreams (e.g. System.out will most likely not be affected).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top