Java中的字面值'\ n'和'\ r'有什么区别? [英] What are the differences between char literals '\n' and '\r' in Java?

查看:249
本文介绍了Java中的字面值'\ n'和'\ r'有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,换行符和回车符似乎都表现出相同的效果。

In Java, the newline and carriage return characters are both seem to be showing same effect.

char文字之间的实际差异是什么 Java中的\ n \ r

What are the actual differences between char literals \n and \r in Java?

请注意,上面询问 \ n 字符,而不是 newLine()函数在 BufferedWriter 上,所以这个其他问题无关紧要。

Note that the above asks about the \n character, not the newLine() function on BufferedWriter, and so this other question isn't relevant.

推荐答案

\ n 是换行符(LF)字符,字符代码为10. \ r 是一个回车符(CR)字符,字符代码13.他们所做的事情因系统而异。例如,在Windows上,使用CR终止文本文件中的行,然后立即使用LF(例如,CRLF)终止。在Unix系统及其衍生产品上,只使用LF。 (Mac OS X之前的Mac使用CR,但Mac OS X是* nix衍生物,所以使用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.)

在过去,LF字面意思只做了一个打印机上的换行(向下移动一行而不移动到页面上的水平位置),CR同样移回行的开头而不用移动纸张,因此某些系统(如Windows) )发送CR(返回到左侧)和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.

这篇关于Java中的字面值'\ n'和'\ r'有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆