如何获得独立于平台的换行符?

发布于 2024-07-07 13:34:28 字数 61 浏览 8 评论 0原文

如何在 Java 中获得独立于平台的换行符? 我不能在任何地方使用 "\n"

How do I get a platform-independent newline in Java? I can’t use "\n" everywhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

最冷一天 2024-07-14 13:34:28

Java 7 现在有一个 System.lineSeparator()< /code>方法。

Java 7 now has a System.lineSeparator() method.

何以畏孤独 2024-07-14 13:34:28

您可以使用

System.getProperty("line.separator");

来获取行分隔符

You can use

System.getProperty("line.separator");

to get the line separator

金兰素衣 2024-07-14 13:34:28

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

参见 Java 1.8 API for Formatter 了解更多详情。

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.

旧时浪漫 2024-07-14 13:34:28

如果您尝试向文件写入换行符,则可以简单地使用 BufferedWriter 的 newLine() 方法。

If you're trying to write a newline to a file, you could simply use BufferedWriter's newLine() method.

短暂陪伴 2024-07-14 13:34:28

commons-lang 库有一个可用的常量字段,称为 SystemUtils.LINE_SEPARATOR

The commons-lang library has a constant field available called SystemUtils.LINE_SEPARATOR

愛放△進行李 2024-07-14 13:34:28
StringBuilder newLine=new StringBuilder();
newLine.append("abc");
newline.append(System.getProperty("line.separator"));
newline.append("def");
String output=newline.toString();

无论平台如何,上面的代码片段都会有两个由新行分隔的字符串。

StringBuilder newLine=new StringBuilder();
newLine.append("abc");
newline.append(System.getProperty("line.separator"));
newline.append("def");
String output=newline.toString();

The above snippet will have two strings separated by a new line irrespective of platforms.

蛮可爱 2024-07-14 13:34:28

从 JDK 1.1 开始,BufferedWriter 类具有“newLine()”方法,用于写入与平台相关的换行符。 它还提供了 StringWriter 类,使得提取新行成为可能:

public static String getSystemNewLine() {
    try {
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);
        bw.newLine();
        bw.flush();
        String s = sw.toString();
        bw.close();
        return s;
    } catch (Exception e) {
        throw new Error(e);
    }
}

Since JDK 1.1, the BufferedWriter class had the "newLine()" method which wrote the platform-dependent new line. It also provided the StringWriter class, making it possible to extract the new line:

public static String getSystemNewLine() {
    try {
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);
        bw.newLine();
        bw.flush();
        String s = sw.toString();
        bw.close();
        return s;
    } catch (Exception e) {
        throw new Error(e);
    }
}
十二 2024-07-14 13:34:28

避免使用 String + String 等附加字符串,而是使用 StringBuilder。

String separator = System.getProperty( "line.separator" );
StringBuilder lines = new StringBuilder( line1 );
lines.append( separator );
lines.append( line2 );
lines.append( separator );
String result = lines.toString( );

Avoid appending strings using String + String etc, use StringBuilder instead.

String separator = System.getProperty( "line.separator" );
StringBuilder lines = new StringBuilder( line1 );
lines.append( separator );
lines.append( line2 );
lines.append( separator );
String result = lines.toString( );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文