【问题标题】:java creating file weird character in notepadjava在记事本中创建文件奇怪的字符
【发布时间】:2015-11-26 08:40:00
【问题描述】:

我正在创建一个文件

File file = new File("myFile.txt");
FileWriterWithEncoding writer;  
writer = new FileWriterWithEncoding(file,"UTF-8", true);
PrintWriter printer = new PrintWriter(writer);
printer.write(myString)
myString contains the word VÄTER

当我用记事本打开这个文件时,我有一个词:VÄTER
如果我用记事本++打开文件,我有:VÄTER

有没有办法在 Java 中创建文件以便在记事本中可以很好地阅读 (VÄTER) 这个单词?

谢谢

【问题讨论】:

  • 我认为您缺少 BOM(字节顺序标记)。例如:stackoverflow.com/questions/4389005/…
  • 你真的不应该使用普通的记事本来做任何事情。它对 Windows CP-1252 编码以外的任何东西的支持都非常差(至少上次我不得不用它做任何事情),所以任何用 UTF-8 或其他编码编写的东西很可能会显示为乱码。
  • 看看建议的答案...

标签: java file


【解决方案1】:

如建议:您需要添加 BOM 字符... 添加 printer.write('\ufeff'); 将完成工作...

 String s = "";
 s = "TELEGRÄMERß : Ünique Cöllection";
 File file = new File("myFile.txt");
 FileWriterWithEncoding writer;
 writer = new FileWriterWithEncoding(file, "UTF-8", true);
 PrintWriter printer = new PrintWriter(writer);
 printer.write('\ufeff'); //  <-- HERE is the key
 printer.write(s);
 // ....
 printer.close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 2018-03-15
    • 1970-01-01
    • 2012-06-15
    相关资源
    最近更新 更多