char 文字 '\n' 之间有什么区别?和'\r'在Java中?
在Java中,换行符和回车符似乎都显示出相同的效果。
Java 中的 char 文字 \n
和 \r
之间的实际区别是什么?
请注意,上面询问的是 \n
字符,而不是 BufferedWriter
上的 newLine()
函数,等等这个其他问题不相关。
In Java, the newline and carriage return characters are both seem to be showing same effect.
What are the actual differences between char literals \n
and \r
in Java?
Note that the above asks about the \n
character, not the newLine()
function on BufferedWriter
, and so this other question isn't relevant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
\n
是换行(LF)字符,字符代码 10。\r
是回车(CR)字符,字符代码 13。它们的作用与系统不同到系统。例如,在 Windows 上,文本文件中的行使用 CR 后紧跟 LF(例如 CRLF)来终止。在 Unix 系统及其衍生系统上,仅使用 LF。 (Mac OS X 之前的 Mac 使用 CR,但 Mac OS X 是 *nix 的衍生版本,因此使用 LF。)在过去,LF 实际上只是在打印机上进行换行(向下移动一行而不水平移动位置)页面上),并且 CR 同样移回到行首,而不将纸张向上移动,因此某些系统(如 Windows)发送 CR(返回到左侧)和 LF(并向上送纸)。
由于所有这些混乱,某些输出目标将接受多个换行符序列,因此您可以从任一字符看到相同的效果,具体取决于您要输出的内容。
\n
is a line feed (LF) character, character code 10.\r
is a carriage return (CR) character, character code 13. What they do differs from system to system. On Windows, for instance, lines in text files are terminated using CR followed immediately by LF (e.g., CRLF). On Unix systems and their derivatives, only LF is used. (Macs prior to Mac OS X used CR, but Mac OS X is a *nix derivative and so uses LF.)In the old days, LF literally did just a line feed on printers (moving down one line without moving where you are horizonally on the page), and CR similarly moved back to the beginning of the line without moving the paper up, hence some systems (like Windows) sending CR (return to the left-hand side) and LF (and feed the paper up).
Because of all this confusion, some output targets will accept multiple line break sequences, so you could see the same effect from either character depending on what you're outputting to.
\n
适用于 unix\r
适用于 Mac(OS X 之前)\r\n
适用于 Windows 格式,您也可以采用
System.getProperty("line.separator")
它将为您提供适合您的操作系统的格式
\n
is for unix\r
is for mac (before OS X)\r\n
is for windows formatyou can also take
System.getProperty("line.separator")
it will give you the appropriate to your OS
这取决于您工作的平台。要获得正确的结果,请使用 -
It depends on which Platform you work. To get the correct result use -
区别不是 Java 特定的,而是平台特定的。
历史上,类 UNIX 操作系统使用
\n
作为换行符,其他一些已弃用的操作系统使用\r
,而 Windows 操作系统则使用\r\n
>。The difference is not Java-specific, but platform specific.
Historically UNIX-like OSes have used
\n
as newline character, some other deprecated OSes have used\r
and Windows OSes have employed\r\n
.它实际上取决于用于打印结果的内容。通常,结果是相同的,正如您所说 -
从历史上看,回车符应该执行
home
按钮的操作:将插入符号返回到行的开头。\n
应该给你一个新行但不移动插入符。如果您想到旧打印机,您几乎会想到字符集的原始作者的想法。移动送纸器和移动插入符是不同的操作。这两个字符表达了这种差异。
It actually depends on what is being used to print the result. Usually, the result is the same, just as you say -
Historically carriage return is supposed to do about what the
home
button does: return the caret to the start of the line.\n
is supposed to give you a new line but not move the caret.If you think about old printers, you're pretty much thinking how the original authors of the character sets were thinking. It's a different operation moving the paper feeder and moving the caret. These two characters express that difference.
以上提供的答案是完美的。 LF(
\n
)、CR(\r
) 和 CRLF(\r\n
) 字符与平台相关。但是,这些字符的解释不仅由平台定义,还由您使用的控制台定义。在 Intellij 控制台 (Windows) 中,此语句System.out.print("Happ\ry");
中的这个\r
字符生成输出y.但是,如果您使用终端 (Windows),您将得到
yapp
作为输出。The above answers provided are perfect. The LF(
\n
), CR(\r
) and CRLF(\r\n
) characters are platform dependent. However, the interpretation for these characters is not only defined by the platforms but also the console that you are using. In Intellij console (Windows), this\r
character in this statementSystem.out.print("Happ\ry");
produces the outputy
. But, if you use the terminal (Windows), you will getyapp
as the output.当您在控制台(Eclipse)中打印字符串时,\n、\r 和 \r\n 具有相同的效果,它们都会给您一个新行;但是 \n\r(也 \n\n、\r \r) 会给你两个新行;当你将一个字符串写入文件时,只有 \r\n 可以给你一个新行。
When you print a string in console(Eclipse),\n,\r and \r\n have the same effect,all of them will give you a new line;but \n\r(also \n\n,\r\r) will give you two new lines;when you write a string to a file,only \r\n can give you a new line.
在命令行上,\r 会将光标移回到当前行的开头。要查看差异,您必须从命令提示符运行代码。 Eclipse 的控制台显示这两个表达式的类似输出。
有关转义序列的完整列表,请单击此处 https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.6
On the command line, \r will move the cursor back to the beginning of the current line. To see the difference you must run your code from a command prompt. Eclipse's console show similar output for both the expression.
For complete list of escape sequences, click here https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.6