你知道为什么下面的第一行和第二行不产生文件的 BOM 而第三行吗?我认为 UTF-16LE 是正确的编码名称,并且该编码不会自动创建 BOM 到文件的开头。
new File("foo-wo-bom.txt").withPrintWriter("utf-16le") {it << "test"}
new File("foo-bom1.txt").withPrintWriter("UnicodeLittleUnmarked") {it << "test"}
new File("foo-bom.txt").withPrintWriter("UTF-16LE") {it << "test"}
另一个样品
new File("foo-bom.txt").withPrintWriter("UTF-16LE") {it << "test"}
new File("foo-bom.txt").getBytes().each {System.out.format("%02x ", it)}
印刷
ff fe 74 00 65 00 73 00 74 00
和java
PrintWriter w = new PrintWriter("foo.txt","UTF-16LE");
w.print("test");
w.close();
FileInputStream r = new FileInputStream("foo.txt");
int c;
while ((c = r.read()) != -1) {
System.out.format("%02x ",c);
}
r.close();
印刷
74 00 65 00 73 00 74 00
Java 不会产生 BOM,而 Groovy 会产生 BOM。