我如何获得平台相关的新行字符?


如何在Java中获得依赖于平台的换行符?我无法在任何地方使用“ n”

除了line.separator属性之外,如果您正在使用java 1.5或更高版本以及 String.format (或其他
格式 方法),则可以使用%n

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   ^^

请参阅 Formatter的Java 1.8 API了解更多详情。

您可以使用

System.getProperty("line.separator");

得到行分隔符

未经作者同意,本文严禁转载,违者必究!