Java中的回车\换行

发布于 2024-09-01 08:52:14 字数 730 浏览 11 评论 0原文

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

为了编写文本文件,我使用 java.io.FileWriter 和 BufferedWriter。对于每行后的换行符,我使用 bw.newLine() 方法(其中 bwBufferedWriter 的对象)。

我通过从 Unix 环境本身附加邮件来发送该文本文件(使用 Unix 命令自动执行)。

我的问题是,在Windows系统中从邮件下载文本文件后,如果我 打开该文本文件,数据未正确对齐。 newline() 字符是 不工作,我想是的。

我想要与 Unix 环境中相同的文本文件对齐方式,如果我打开 Windows 环境中的文本文件也是如此。

我该如何解决这个问题?

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

I have created a text file in Unix environment using Java code.

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).

And I'm sending that text file by attaching in mail from Unix environment itself (automated that using Unix commands).

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.

I want same text file alignment as it is in Unix environment, if I opened the
text file in Windows environment also.

How do I resolve the problem?

Java code below for your reference (running in Unix environment):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

四叶草在未来唯美盛开 2024-09-08 08:52:14

Java 只知道它当前运行的平台,因此它只能为您提供该平台上依赖于平台的输出(使用 bw.newLine())。事实上,您在 Windows 系统上打开它意味着您必须在使用它之前转换该文件(使用您编写的内容,或者使用诸如 unix2dos),或者你必须在你的Java程序中输出带有windows格式回车的文件。因此,如果您知道该文件始终会在 Windows 计算机上打开,则必须输出

bw.write(rs.getString(1)==null? "":rs.getString(1));
bw.write("\r\n");

值得注意的是,如果您只是纯文本,则您将无法输出在两个平台上看起来正确的文件如果是电子邮件,您可能需要考虑使用 html,如果是数据,您可能需要考虑使用 xml。或者,您可能需要某种客户端来读取数据,然后针对查看者正在使用的平台对其进行格式化。

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

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.

北音执念 2024-09-08 08:52:14

newLine() 方法确保添加平台兼容的新行(对于 DOS,0Dh 0Ah,对于旧版 Mac,0Dh0Ah 对于 Unix/Linux)。 Java 无法知道您要在哪个平台上发送文本。此转换应由邮件发送实体负责。

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.

屌丝范 2024-09-08 08:52:14

不知道谁查看您的文件,但如果您在写字板而不是记事本中打开它,换行符将显示正确。如果您使用特殊的文件扩展名,请将其与写字板关联起来,然后就完成了。或者使用任何其他更高级的文本编辑器。

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.

初见终念 2024-09-08 08:52:14

bw.newLine(); 无法确保与所有系统的兼容性。

如果你确定要在windows中打开它,你可以将其格式化为windows换行符。

如果您已经在使用本机 unix 命令,请尝试 unix2dos 并将已生成的文件转换为 Windows 格式,然后发送邮件。

如果您不使用 unix 命令并且更喜欢在 java 中执行此操作,请使用“bw.write("\r\n")`,如果它不会使您的程序复杂化,请使用一种方法来查找操作系统并写入适当的换行符。

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.

If you are already using native unix commands, try unix2dos and convert teh already generated file to windows format and then send the mail.

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.

紫轩蝶泪 2024-09-08 08:52:14

如果我没理解错的话,我们讨论的是文本文件附件。
不幸的是,因为如果它是电子邮件的消息正文,您始终可以使用“\r\n”,指的是 http://www.faqs.org/rfcs/rfc822.html

但由于它是附件,因此您必须忍受系统差异。如果我处在你的位置,我会选择以下选项之一:

a) 仅支持 Windows 客户端,使用“\r\n”作为行结束符。

b) 提供两个附件文件,一份为linux格式,一份为windows格式。

c) 我不知道附件是由人还是机器阅读,但如果是人,我会考虑附加 HTML 文件而不是纯文本。更便携,也更漂亮:)

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

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.

b) provide two attachment files, one with linux format and one with windows format.

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 :)

桃酥萝莉 2024-09-08 08:52:14

封装您的编写器以提供字符替换,如下所示:

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文