System.lineSeparator()行分隔符的用法

System.lineSeparator()具体含义

从JDK的源码中,可以看出:它是从JDK1.7之后开始有的这个方法。
在UNIX系统下,System.lineSeparator()方法返回"\n"
在Windows系统下,System.linSeparator()方法返回"\r\n"

下面是源码:

    /**
     * Returns the system-dependent line separator string.  It always
     * returns the same value - the initial value of the {@linkplain
     * #getProperty(String) system property} {@code line.separator}.
     *
     * <p>On UNIX systems, it returns {@code "\n"}; on Microsoft
     * Windows systems it returns {@code "\r\n"}.
     *
     * @return the system-dependent line separator string
     * @since 1.7
     */
    public static String lineSeparator() {
        return lineSeparator;
    }

    private static String lineSeparator;

代码中不要硬编码"\n","\r",建议使用System.linSeparator()获取运行环境行分隔符。

posted @ 2022-05-23 20:14  cavan丶keke  阅读(861)  评论(0编辑  收藏  举报