java中的回车(“\r\n”)

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

Carriage Return("\r\n") in java

java

提问by Manu

I have a created text file in unix enviroment using java code.

我在 unix 环境中使用 java 代码创建了一个文本文件。

For writing the text file i am using java.io.FileWriter and BufferedWriter. and for newline after each row i am using bw.write("\r\n") method. (where bw is object of BufferedWriter) and sending that text file by attaching in mail from unix environment itself to other environments like(mainframe,windows)

为了编写文本文件,我使用 java.io.FileWriter 和 BufferedWriter。对于每一行后的换行符,我使用 bw.write("\r\n") 方法。(其中 bw 是 BufferedWriter 的对象)并通过将邮件从 unix 环境本身附加到其他环境(例如(大型机,windows))来发送该文本文件

My issue is, if my client's download the text file in Mainframe system, they found that in text file a "special character"(like small rectangular box) presenting and data not properly aligned.

我的问题是,如果我的客户在大型机系统中下载文本文件,他们发现文本文件中有一个“特殊字符”(如小矩形框)呈现且数据未正确对齐。

bw.write("\r\n") is not working i think so..(but working fine in windows).

bw.write("\r\n") 不起作用我认为是这样..(但在 Windows 中工作正常)。

I want same text file alignment as it is in unix environment and without any special
character symbol too, if they opened the text file in mainframe ,windows environments or any other enviroments.


如果他们在大型机、windows 环境或任何其他环境中打开文本文件,我想要与 unix 环境中相同的文本文件对齐方式,并且也没有任何特殊字符符号。

How to resolve the problem. Thanks for your help in advance.

如何解决问题。提前感谢您的帮助。

pasting my piece of java code here for your reference..(running java code in unix
environment)

在这里粘贴我的一段 java 代码供您参考..(在 unix
环境中运行 java 代码)

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 Jon Skeet

Contrary to your claim, your code clearly isn'twriting "\r\n" after each line. It's calling BufferedWriter.newLine()which uses the current platform's default line separator (unless you explicitly set the line.separatorsystem property). On Unix systems this will be "\n".

与您的主张相反,您的代码显然没有在每一行之后写上“\r\n”。它正在调用BufferedWriter.newLine()它使用当前平台的默认行分隔符(除非您明确设置line.separator系统属性)。在 Unix 系统上,这将是“\n”。

Now, you need to think about what you want the result to be on the targetsystems where it will be read. For Windows, that means you shouldexplicitly write "\r\n"... I don't know about the mainframes you mention though - you'd need to give more details.

现在,您需要考虑您希望结果在将被读取的目标系统上是什么。对于 Windows,这意味着你应该明确地写出“\r\n”......我不知道你提到的大型机 - 你需要提供更多细节。

Note that the fact that there are different line separators available means that no one text file will be universally suitable. Many text editors on different platforms cope with any line separator... but there will always be some that don't :(

请注意,有不同的行分隔符可用这一事实意味着没有一个文本文件是普遍适用的。不同平台上的许多文本编辑器都可以处理任何行分隔符......但总会有一些不:(

回答by Carl Smotricz

To write a file with environmentally correct line endings, the recommended class is PrintWriter. The println(String)method will output the given string and the appropriate line ending to the output file.

要编写具有环境正确行尾的文件,推荐的类是PrintWriter. 该println(String)方法将输出给定的字符串和适当的行结尾到输出文件。



Jon Skeet is right, though: Your problem isn't so much technical as figuring out which file format you need on which system. If a file is processed on a Windows system, it is expected that lines end in \r\n; on Unix systems, just \n.

不过,Jon Skeet 是对的:您的问题不是技术问题,而是弄清楚您需要在哪个系统上使用哪种文件格式。如果在 Windows 系统上处理文件,则行应该以\r\n;结尾。在 Unix 系统上,只是\n.

All this is complicated even more when you use FTP to transfer files between different systems: ASCII mode will automatically translate line endings for you, binary will not.

当您使用 FTP 在不同系统之间传输文件时,所有这些都变得更加复杂:ASCII 模式会自动为您翻译行尾,而二进制不会。

Email is also not kind to text formats: Outlook frequently mutilates text files sent across system boundaries.

电子邮件也不喜欢文本格式:Outlook 经常破坏跨系统边界发送的文本文件。

People here on SO know about this kind of stuff. Give us some more details to work with and we can propose solutions. But it will likely not be possible to write a file on one system that will be correctly read on any system.

SO上的人们知道这种东西。给我们一些更多的细节,我们可以提出解决方案。但是可能无法在一个系统上写入在任何系统上都能正确读取的文件。