我是靠谱客的博主 幸福雪糕,这篇文章主要介绍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换行内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部