概述
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执行此简单程序会导致回车/换行文件不同?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复