Java newLine() 和回车("\r") 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3307747/
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-13 21:57:41  来源:igfitidea点击:

difference between newLine() and carriage return("\r")

java

提问by Manu

What is the difference between newLine()and carriage return ("\r")? Which one is best to use?

newLine()和回车(“\r”)有什么区别?最好使用哪一种?

File f = new File(strFileGenLoc);
BufferedWriter bw = new BufferedWriter(new FileWriter(f, false));
rs = stmt.executeQuery("select * from jpdata");
while ( rs.next() ) 
{
    bw.write(rs.getString(1)==null? "":rs.getString(1));
    bw.newLine();
}

采纳答案by Rob Stevenson-Leggett

Assuming you mean this:

假设你的意思是:

public static String newLine = System.getProperty("line.separator");

newLineis environment agnostic \risn't.

newLine是环境不可知\r的不是。

So newLinewill give you \r\non windows but \non another environment.

所以newLine会给你\r\n在 Windows 上但\n在另一个环境中。

However, you shouldn't use this in a JTextArea and println will work fine with just \non windows.

但是,您不应在 JTextArea 中使用它,而 println 仅\n在 Windows 上也能正常工作。

Edit now that I've seen the code and your comment

现在编辑我已经看到了代码和你的评论

In your situation. I think you should use your own constant - \r\n

在你的情况下。我认为您应该使用自己的常量 -\r\n

  File f = new File(strFileGenLoc);
  BufferedWriter bw = new BufferedWriter(new FileWriter(f, false));
  rs = stmt.executeQuery("select * from jpdata");
  while ( rs.next() ) {
    bw.write(rs.getString(1)==null? "":rs.getString(1));
    bw.write("\r\n");
  }

回答by Ahmed Aman

Write a line separator. The line separator string is defined by the system property line.separator, and is not necessarily a single newline ('\n') character.

写一个行分隔符。行分隔符字符串由系统属性 line.separator 定义,不一定是单个换行符 ('\n')。

source: http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/io/BufferedWriter.html

来源:http: //download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/io/BufferedWriter.html

回答by user207421

In the old days of ASR-33 teletypes (and, later, dot-matrix printers with travelling print-heads), the CR literally returned the carriage to the left, as in a typewriter, and the LF advanced the paper. The machinery could overlap the operations if the CR came before the LF, so that's why the newline character was always CR-LF, i.e. \r\n. If you got it back to front it took much longer to print. Unix was the first (only?) system to adopt just \n as the standard line separator. DOS/Windows didn't.

在 ASR-33 电传打字机(以及后来的带有移动打印头的点阵打印机)的旧时代,CR 确实将托架返回到左侧,就像在打字机中一样,而 LF 则推进纸张。如果 CR 出现在 LF 之前,机器可能会重叠操作,这就是为什么换行符总是 CR-LF,即 \r\n。如果你把它放回前面,打印需要更长的时间。Unix 是第一个(唯一的?)系统只采用 \n 作为标准行分隔符。DOS/Windows 没有。

回答by charan

the major newLine(\n) and carriage return (\r) is :

主要的 newLine( \n) 和回车 ( \r) 是:

new line(\n) & carriage return (\r) both r escape sequence in java..

新行(\n)和回车(\r)都在java中的r转义序列..

new line(\n):

新行(\n):

when ever \nis found in executable statements,the JVMsplits the line into 2 parts from where we placed \n(escape sequence).. example :

\n在可执行语句中找到时,JVM将行从我们放置的位置\n(转义序列)分成两部分。例如:

println("Stackover\nflow");

output:

输出:

Stackover // here the line got split into 2 lines because of "\n"   
flow

carriage return (\r):

回车 ( \r):

the data or character placed after the \rget overtire with the previous characters present in the same line ...

\rget overtire之后放置的数据或字符与同一行中存在的先前字符...

example :

例子 :

println("Stackover\rflow"); 
output:
flow  // here Statckover is overtired  because of "\r" 

回答by Zay Ya

On Windows platforms, the line separator is CRLF (carriage return / line feed), which is "\r\n". On Unix platforms, it's just LF, "\n". Traditionally, on Apple platforms it has been just CR, "\r".

在 Windows 平台上,行分隔符是 CRLF(回车/换行),即“\r\n”。在 Unix 平台上,它只是 LF,“\n”。传统上,在 Apple 平台上,它只是 CR,“\r”。