Writing to a text file with bytes and BOM with UTF-8 in Java

I love UTF-8.

This is the code I have:
final File file = new File("hello.txt");
final FileOutputStream fileOutputStream = new FileOutputStream(file);
final DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);
 
dataOutputStream.writeByte(0xEF);
dataOutputStream.writeByte(0xBB);
dataOutputStream.writeByte(0xBF);
 
dataOutputStream.writeByte(65);
dataOutputStream.writeByte(66);
 
dataOutputStream.flush();
dataOutputStream.close();

And when I open the file with Sublime Text, I will see:


The first 3 bytes we have written to the file is the BOM for UTF-8 files. You can learn more about it here.