【发布时间】:2016-02-18 21:23:41
【问题描述】:
我已经检查了所有可用于本地 Windows 的源代码 CreateFileW 和 WriteFile 在 jdk8u/jdk/src/windows/native/sun/nio/ch/FileDispatcherImpl.c 中调用,但我不知道为什么这两个调用会创建具有不同长度的文件:
FileOutputStream f = new FileOutputStream(new File("x.cfg"));
f.write("hello\n".getBytes());
f.close();
和
FileOutputStream f = new FileOutputStream(new File("x.txt"));
f.write("hello\n".getBytes());
f.close();
我尝试了不同的扩展,但似乎只有.cfg导致LF→CRLF转换...
看起来这不是 Java 的问题,只是 Windows 的问题。
【问题讨论】:
-
使用 JDK1.8.0_65 这两个文件在我的系统上都有 6 个字节(Windows 7 64 位)。内容可能被病毒扫描程序更改了吗?
-
我试过JDK1.8.0_66,Windows 2012R2
-
您应该检查是否分配了 config.properties 文件。我不确定具体细节,但这里有一个用法链接crunchify.com/…
-
这是 Java 问题,而不是 Windows 问题。 WriteFile 不转换任何内容,CreateFile 不提供指定 text 输出的方法。所有必需的转换都必须在 Windows API(即 CRT 或 JVM)之上实现。