質問

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