我是靠谱客的博主 幸福雪糕,最近开发中收集的这篇文章主要介绍os.write换行 java,为什么用Java和AIX执行此简单程序会导致回车/换行文件不同?,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

If I run this simple program on Windows 7 and then on AIX (Unix system) and compare the two generated files using a tool such as Winmerge or Compare It, it tells me that the Carriage Return and Line Feed are different but the content identical.

Why is that? Isn't supposed to be the same if both use the same encoding "UTF-8" in this case?

How can I make both files totally equal?

public class WriteFile {

public static void main(String[] args) throws IOException {

write();

}

public static void write() throws IOException {

File file = new File("/tmp/test.txt");

file.delete();

file.createNewFile();

String str = "hello";

FileOutputStream fileOs = new FileOutputStream(file);

PrintWriter writer = new PrintWriter(new OutputStreamWriter(fileOs, "UTF-8"), true);

writer.println(str);

writer.close();

}

}

解决方案

Different operating systems use different newline conventions:

On Windows, newlines are CR+LF;

On Unix, newlines are LF.

(If you're curious, there's more.).

If you need the output files to be identical, don't use println(), but write n or rn instead:

writer.printf("%sn", str); // LF

writer.printf("%srn", str); // CR+LF

最后

以上就是幸福雪糕为你收集整理的os.write换行 java,为什么用Java和AIX执行此简单程序会导致回车/换行文件不同?的全部内容,希望文章能够帮你解决os.write换行 java,为什么用Java和AIX执行此简单程序会导致回车/换行文件不同?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(86)

评论列表共有 0 条评论

立即
投稿
返回
顶部