Java System.getProperty(“line.separator”)之间的差异;及\n"?

Java System.getProperty(“line.separator”)之间的差异;及\n"?,java,javafx-8,file-handling,Java,Javafx 8,File Handling,在用JavaFX开发GUI时,我似乎用System.getProperty(“line.separator”)得到了不同的结果;和“\n”在写入文件或从internet获取数据期间。基本区别是什么?在Windows平台上,System.getProperty(“line.separator”)是“\r\n”, “\n”(Linux和MacOS X)、“\r”(MacOS 9及更早版本)System.getProperty(“line.separator”)依赖于平台: UNIX样式计算机上的“

在用JavaFX开发GUI时,我似乎用System.getProperty(“line.separator”)得到了不同的结果;和“\n”在写入文件或从internet获取数据期间。基本区别是什么?

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

System.getProperty(“line.separator”)
依赖于平台:

  • UNIX样式计算机上的“\n”
  • Windows计算机上的“\r\n”
而“\n”只是“\n”。

System.getProperty(“line.separator”)
返回操作系统相关的行分隔符


在Windows上返回
“\r\n”
,在Unix上返回
“\n”
。因此,如果要为当前操作系统生成具有行尾的文件,请使用
System.getProperty(“line.separator”)
或使用
PrintWriter
«\n»作为大多数操作系统(如Linux/Unix)的行分隔符。为确保与任何操作系统兼容,请使用system.getproperty查询此值。有几个密切相关的问题。我不确定一个是否完全符合复制品的条件,但至少,你的问题基本上在这里得到了回答:那么,你认为哪一个更好?请建议。@TilakMadichetti取决于用例。如果您想为当前操作系统换行,请使用第一种形式。你想写什么?我想把一大堆单词写进一个文件,每个单词都有一个行距。@TilakMadichetti如果你用
\n
生成换行符,并在windows上用一个坏编辑器(比如记事本)打开文件,换行符将不会显示。但是好的编辑器同样能够处理unix和windows换行符…我对您提到较旧的
\r
换行符表示赞赏!