2

I am creating a file

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

When I open this file with notepad I have the word :VÄTER
And if I open the file with notepad++ I have : VÄTER

Is there a way to create the file in Java in order that this word can be well read (VÄTER) in notepad?

Thanks

user1260928
  • 3,269
  • 9
  • 59
  • 105
  • 1
    I think you are missing a BOM (Byte Order Mark). Look at this for example: http://stackoverflow.com/questions/4389005/how-to-add-a-utf-8-bom-in-java – pinturic Nov 26 '15 at 08:42
  • You really shouldn't use the regular notepad for anything. It has very poor support for anything beyond Windows' CP-1252 encoding (at least the last time I've had to do anything with it), so anything written in UTF-8 or other encodings will most likely show garbled. – Kayaman Nov 26 '15 at 09:00
  • take a look at the answer suggested... – ΦXocę 웃 Пepeúpa ツ Nov 26 '15 at 09:14

1 Answers1

0

As suggested: you need to add BOM chars... adding printer.write('\ufeff'); will do the work...

 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();
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97