Java System.getProperty("line.separator"); 的区别 和“\n”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36796136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:42:09  来源:igfitidea点击:

Difference between System.getProperty("line.separator"); and "\n" ?

javajavafx-8file-handling

提问by Tilak Maddy

While developing GUI with Java FX , I seem to get different results with System.getProperty("line.separator"); and "\n" during writing to a file or getting data from internet. What basically is the difference ?

在使用 Java FX 开发 GUI 时,我似乎使用 System.getProperty("line.separator"); 得到了不同的结果。和 "\n" 在写入文件或从互联网获取数据期间。基本上有什么区别?

采纳答案by wero

System.getProperty("line.separator")returns the OS dependent line separator.

System.getProperty("line.separator")返回依赖于操作系统的行分隔符。

On Windows it returns "\r\n", on Unix "\n". So if you want to generate a file with line endings for the current operating systems use System.getProperty("line.separator")or write using a PrintWriter.

在 Windows 上它返回"\r\n",在 Unix 上"\n"。因此,如果要为当前操作系统生成带有行结尾的文件,请使用System.getProperty("line.separator")或使用PrintWriter.

回答by EL missaoui habib

on the Windows platform, System.getProperty("line.separator") is "\r\n", "\n" (Linux and MacOS X), "\r" (MacOS 9 and older)

在 Windows 平台上, System.getProperty("line.separator") 是 "\r\n"、"\n"(Linux 和 MacOS X)、"\r"(MacOS 9 及更早版本)

回答by Dan W

System.getProperty("line.separator")is platform dependent:

System.getProperty("line.separator")是平台相关的:

  • "\n" on UNIX style machines
  • "\r\n" on Windows machines
  • "\n" 在 UNIX 风格的机器上
  • "\r\n" 在 Windows 机器上

Whereas "\n" is only "\n".

而“\n”只是“\n”。

回答by Mario

?\n? is the line separator for most operating systems such as Linux/Unix. To ensure the compatibility with any operating system, query this value with System.getproperty

?\n? 是大多数操作系统(如 Linux/Unix)的行分隔符。为确保与任何操作系统兼容,请使用 System.getproperty 查询此值