java java中的换行符读取

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

linefeed character reading in java

javacarriage-returnlinefeed

提问by Ravi.Kumar

I am wondering that when I open a file in notepad. I see a continuous line without any carriage return/line feed.

我想知道当我在记事本中打开文件时。我看到一条没有任何回车/换行的连续线。

I made a java program to read the file. When I split the data from file by using \nor System.getProperty("line.separator");. I see lots of lines.

我做了一个java程序来读取文件。当我使用\n或拆分文件中的数据时System.getProperty("line.separator");。我看到很多线。

I found in hex editor that file has '0A' for new line ( used in UNIX ) and it appears as a rectangle in Notepad.

我在十六进制编辑器中发现该文件的换行符为“0A”(在 UNIX 中使用),它在记事本中显示为一个矩形。

Well, my question is that if it doesn't have '0D' and 'OA' ( used in Windows for carriage return and line feed ). How my java program is splitting the data into lines? It should not split it.

好吧,我的问题是,如果它没有“0D”和“OA”(在 Windows 中用于回车和换行)。我的java程序如何将数据分成几行?它不应该分裂它。

Anyone have any idea?

任何人有任何想法?

回答by Marc-Christian Schulze

Java internally works with Unicode.

Java 在内部使用 Unicode。

The Unicode standard defines a large number of characters that conforming applications should recognize as line terminators:[3]
LF: Line Feed, U+000A
VT: Vertical Tab, U+000B
FF: Form Feed, U+000C
CR: Carriage Return, U+000D
CR+LF: CR (U+000D) followed by LF (U+000A)
NEL: Next Line, U+0085
LS: Line Separator, U+2028
PS: Paragraph Separator, U+2029

Unicode 标准定义了大量符合应用程序应识别为行终止符的字符:[3]
LF:换行,U+000A
VT:垂直制表符,U+000B
FF:换页,U+000C
CR:回车, U+000D
CR+LF:CR (U+000D) 后跟 LF (U+000A)
NEL:下一行,U+0085
LS:行分隔符,U+2028
PS:段落分隔符,U+2029

(http://en.wikipedia.org/wiki/Newline) That's why it interprets \nas newline.

( http://en.wikipedia.org/wiki/Newline) 这就是它解释\n为换行符的原因。

回答by Synesso

The character \nis 0a(carriage return). If you split Windows line separators by \nonly you'll split on the 0a, leaving the 0dcharacters behind.

字符\n0a(回车)。如果\n仅按分隔符分隔 Windows 行分隔符,则会在 上分隔0a,而将0d字符留在后面。

Notepad shows 0aas a square, but it will render 0d0aas a newline.

记事本显示0a为正方形,但它将呈现0d0a为换行符。

Here's an example using Scala (it's Java under the covers) on Windows:

下面是一个在 Windows 上使用 Scala(它是 Java)的例子:

scala> "123\n456".split(System.getProperty("line.separator")).length
res1: Int = 1

scala> "123\n456".split("\r\n").length  // same as the line above on Windows
res2: Int = 1

scala> "123\n456".split("\n").length
res3: Int = 2

回答by ioreskovic

Windows Notepad is something to be strongly avoided when dealing with any type of text file.
I suggest using Notepad++.

在处理任何类型的文本文件时,应强烈避免使用 Windows 记事本。
我建议使用Notepad++

Not only he'll display your text nicely, but it also has a feature to encode the file in UTF-8and without BOM:D

他不仅会很好地显示您的文本,而且还具有以UTF-8格式对文件进行编码且无需BOM 的功能:D