简体   繁体   English

Java中的回车\换行

[英]Carriage Return\Line feed in Java

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 . 为了编写文本文件,我使用的是java.io.FileWriterBufferedWriter And for newline after each row I am using bw.newLine() method (where bw is object of BufferedWriter ). 对于每行之后的换行符,我使用的是bw.newLine()方法(其中bwBufferedWriter对象)。

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. 我的问题是,在我从Windows系统中的邮件下载文本文件后,如果我打开该文本文件,则数据未正确对齐。 newline() character is not working, I think so. 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();
}

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() ) . Java只知道它当前运行的平台,因此它只能在该平台上为您提供依赖于平台的输出(使用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. 您在Windows系统上打开它的事实意味着您必须在使用之前转换文件(使用您编写的内容或使用unix2dos之类的程序),或者您必须输出带有Windows格式的文件回车符它最初是在你的Java程序中。 So if you know the file will always be opened on a windows machine, you will have to output 因此,如果您知道该文件将始终在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. 值得注意的是,如果它只是您正在使用的纯文本,您将无法输出在两个平台上看起来都正确的文件,如果是电子邮件,您可能需要考虑使用html,如果它是xml,则可能需要考虑使用xml是数据。 Alternatively, you may need some kind of client that reads the data and then formats it for the platform that the viewer is using. 或者,您可能需要某种类型的客户端来读取数据,然后将其格式化为查看者正在使用的平台。

The method newLine() ensures a platform-compatible new line is added ( 0Dh 0Ah for DOS, 0Dh for older Macs, 0Ah for Unix/Linux). newLine()方法确保添加平台兼容的新行(DOS为0Dh 0Ah ,旧Mac为0Dh ,Unix / Linux为0Ah )。 Java has no way of knowing on which platform you are going to send the text. Java无法知道您要在哪个平台上发送文本。 This conversion should be taken care of by the mail sending entities. 邮件发送实体应该对此转换进行处理。

Don't know who looks at your file, but if you open it in wordpad instead of notepad, the linebreaks will show correct. 不知道谁看你的文件,但如果你用wordpad而不是记事本打开它,换行符将显示正确。 In case you're using a special file extension, associate it with wordpad and you're done with it. 如果您正在使用特殊的文件扩展名,请将其与wordpad相关联,然后您就完成了它。 Or use any other more advanced text editor. 或使用任何其他更高级的文本编辑器。

bw.newLine(); cannot ensure compatibility with all systems. 无法确保与所有系统的兼容性。

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”)`并且如果它不会使您的程序复杂化,请使用一种方法找出操作系统并写入适当的换行符。

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. c)我不知道附件是由人或机器读取,但如果是人,我会考虑附加HTML文件而不是纯文本。 more portable and much prettier, too :) 更便携,更漂亮:)

Encapsulate your writer to provide char replacement, like this: 封装您的编写器以提供char替换,如下所示:

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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM