This code,
OutputStream out = new FileOutputStream(new File("C:/file/test.txt"));
out.write("A".getBytes());
And this,
OutputStream out = new FileOutputStream(new File("C:/file/test.txt"));
out.write("A".getBytes(StandardCharsets.UTF_8));
produce the same result(in my opinion), which is UTF-8 without BOM. However, Notepad++ is not showing any information about encoding. I'm expecting notepad++ to show here as Encode in UTF-8 without BOM
, but no encoding is being selected in the "Encoding" menu.
Now, this code write the file in UTF-8 with BOM encoding.
OutputStream out = new FileOutputStream(new File("C:/file/test.txt"));
byte[] bom = { (byte) 239, (byte) 187, (byte) 191 };
out.write(bom);
out.write("A".getBytes());
Notepad++ is also displaying the encoding type as Encode in UTF-8
.
Question: What is wrong with the first two codes which are suppose to write the file in UTF-8 without BOM? Is my Java code doing the right thing? If so, is there a problem with notepad++ trying to detect the encoding type?
Is notepad++ only guessing around?