Java中的回车\换行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2832756/
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
Carriage Return\Line feed in Java
提问by Manu
I have created a text file in Unix environment using Java code.
我使用 Java 代码在 Unix 环境中创建了一个文本文件。
For writing the text file I am using java.io.FileWriter
and BufferedWriter
. And for newline after each row I am using bw.newLine()
method (where bw
is object of BufferedWriter
).
为了编写我正在使用的文本文件java.io.FileWriter
和BufferedWriter
. 对于每一行之后的换行符,我正在使用bw.newLine()
方法(其中bw
的对象是BufferedWriter
)。
And I'm sending that text file by attaching in mail from Unix environment itself (automated that using Unix commands).
我通过在来自 Unix 环境本身的邮件中附加邮件来发送该文本文件(使用 Unix 命令自动发送)。
My issue is, after I download the text file from mail in a Windows system, if I
opened that text file the data is not properly aligned. newline()
character is
not working, I think so.
我的问题是,在我从 Windows 系统的邮件中下载文本文件后,如果我打开该文本文件,数据没有正确对齐。newline()
性格不行,我想是的。
I want same text file alignment as it is in Unix environment, if I opened the text file in Windows environment also.
如果我也在 Windows 环境中打开文本文件,我想要与 Unix 环境中相同的文本文件对齐方式。
How do I resolve the problem?
如何解决问题?
Java code below for your reference (running in Unix environment):
以下 Java 代码供您参考(在 Unix 环境中运行):
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 James B
Java only knows about the platform it is currently running on, so it can only give you a platform-dependent output on that platform (using bw.newLine()
) . The fact that you open it on a windows system means that you either have to convert the file before using it (using something you have written, or using a program like unix2dos), or you have to output the file with windows format carriage returns in it originally in your Java program. So if you know the file will always be opened on a windows machine, you will have to output
Java 只知道它当前运行的平台,因此它只能在该平台上为您提供与平台相关的输出(使用bw.newLine()
)。您在 Windows 系统上打开它的事实意味着您必须在使用它之前转换文件(使用您编写的内容,或使用类似unix2dos的程序),或者您必须使用 Windows 格式回车输出文件它最初在您的 Java 程序中。因此,如果您知道该文件将始终在 Windows 机器上打开,您将不得不输出
bw.write(rs.getString(1)==null? "":rs.getString(1));
bw.write("\r\n");
It's worth noting that you aren't going to be able to output a file that will look correct on both platforms if it is just plain text you are using, you may want to consider using html if it is an email, or xml if it is data. Alternatively, you may need some kind of client that reads the data and then formats it for the platform that the viewer is using.
值得注意的是,如果它只是您正在使用的纯文本,您将无法输出在两个平台上看起来都正确的文件,如果是电子邮件,您可能需要考虑使用 html,如果是电子邮件,则可以考虑使用 xml是数据。或者,您可能需要某种客户端来读取数据,然后针对查看器使用的平台对其进行格式化。
回答by nc3b
The method newLine()
ensures a platform-compatible new line is added (0Dh 0Ah
for DOS, 0Dh
for older Macs, 0Ah
for Unix/Linux). Java has no way of knowing on which platform you are going to send the text. This conversion should be taken care of by the mail sending entities.
该方法newLine()
确保添加了一个与平台兼容的新行(0Dh 0Ah
对于 DOS,0Dh
对于较旧的 Mac,0Ah
对于 Unix/Linux)。Java 无法知道您将在哪个平台上发送文本。这种转换应该由邮件发送实体来处理。
回答by Vaishak Suresh
bw.newLine();
cannot ensure compatibility with all systems.
bw.newLine();
无法确保与所有系统兼容。
If you are sure it is going to be opened in windows, you can format it to windows newline.
如果您确定它将在 Windows 中打开,您可以将其格式化为 Windows 换行符。
If you are already using native unix commands, try unix2dos
and convert teh already generated file to windows format and then send the mail.
如果您已经在使用本机 unix 命令,请尝试unix2dos
将已经生成的文件转换为 Windows 格式,然后发送邮件。
If you are not using unix commands and prefer to do it in java, use ``bw.write("\r\n")` and if it does not complicate your program, have a method that finds out the operating system and writes the appropriate newline.
如果您不使用 unix 命令并且更喜欢在 java 中执行,请使用 ``bw.write("\r\n")` 并且如果它不会使您的程序复杂化,请使用一种找出操作系统并写入的方法适当的换行符。
回答by rompetroll
If I understand you right, we talk about a text file attachment. Thats unfortunate because if it was the email's message body, you could always use "\r\n", referring to http://www.faqs.org/rfcs/rfc822.html
如果我理解正确,我们谈论的是文本文件附件。那很不幸,因为如果它是电子邮件的消息正文,您总是可以使用“\r\n”,指的是http://www.faqs.org/rfcs/rfc822.html
But as it's an attachment, you must live with system differences. If I were in your shoes, I would choose one of those options:
但作为一个附件,你必须忍受系统差异。如果我是你,我会选择以下选项之一:
a) only support windows clients by using "\r\n" as line end.
a) 仅通过使用“\r\n”作为行尾来支持 Windows 客户端。
b) provide two attachment files, one with linux format and one with windows format.
b) 提供两个附件文件,一个是linux格式的,一个是windows格式的。
c) I don't know if the attachment is to be read by people or machines, but if it is people I would consider attaching an HTML file instead of plain text. more portable and much prettier, too :)
c) 我不知道附件是供人阅读还是由机器阅读,但如果是人,我会考虑附上 HTML 文件而不是纯文本。更便携,更漂亮,太:)
回答by Axel
Don't know who looks at your file, but if you open it in wordpad instead of notepad, the linebreaks will show correct. In case you're using a special file extension, associate it with wordpad and you're done with it. Or use any other more advanced text editor.
不知道谁在看你的文件,但如果你用写字板而不是记事本打开它,换行符会显示正确。如果您使用的是特殊的文件扩展名,请将其与写字板相关联,然后就完成了。或者使用任何其他更高级的文本编辑器。
回答by The Felis Leo
Encapsulate your writer to provide char replacement, like this:
封装您的编写器以提供字符替换,如下所示:
public class WindowsFileWriter extends Writer {
private Writer writer;
public WindowsFileWriter(File file) throws IOException {
try {
writer = new OutputStreamWriter(new FileOutputStream(file), "ISO-8859-15");
} catch (UnsupportedEncodingException e) {
writer = new FileWriter(logfile);
}
}
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
writer.write(new String(cbuf, off, len).replace("\n", "\r\n"));
}
@Override
public void flush() throws IOException {
writer.flush();
}
@Override
public void close() throws IOException {
writer.close();
}
}