How do I get a platform
How do I get a platform-dependent newline in Java? I can't use "n"
everywhere.
In addition to the line.separator property, if you are using java 1.5 or later and the String.format (or other formatting methods) you can use %n
as in
Calendar c = ...;
String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY%n", c);
//Note `%n` at end of line ^^
String s2 = String.format("Use %%n as a platform independent newline.%n");
// %% becomes % ^^
// and `%n` becomes newline ^^
See the Java 1.8 API for Formatter for more details.
You can use
System.getProperty("line.separator");
to get the line separator
Java 7现在有一个System.lineSeparator()
方法。
下一篇: 我如何获得平台