Carriage Return \ Java的源码 [英] Carriage Return\Line feed in Java

查看:128
本文介绍了Carriage Return \ Java的源码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

为了编写文本文件我正在使用 java.io.FileWriter BufferedWriter 。对于每行后的换行符,我使用 bw.newLine()方法(其中 bw 是<$ c的对象$ c> BufferedWriter )。

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

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

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

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

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.

我想要相同的文本文件对齐因为它在Unix环境中,如果我也在Windows环境中打开
文本文件。

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

如何解决问题?

以下Java代码供您参考(在Unix环境下运行):

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


推荐答案

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

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");

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

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.

这篇关于Carriage Return \ Java的源码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆