Write UTF-8 BOM marker char8s) at the start of file?

X

X_AWieminer_X

This is what Ive created to solve UTF-8 BOM bug in InputStreamReader.
http://koti.mbnet.fi/akini/java/unicodereader/

Now, what is the best way to write a new file with UTF-8 BOM mark.
Currently I do it this way but it looks silly to me. First, I create a
regular fileoutputstream and write byte array. Then I instantiate a
outputstreamwriter and continue writing the real data on it.

Writer has only char array methods and write(int) method, but I dont
know if can use it to create 3-bytes BOM mark EFBBBF.

thx

[Current code]
public void saveFile(String file, String data) throws IOException {
// Use UTF-8 with BOM mark format
FileOutputStream fos = null;
OutputStreamWriter w = null;
try {
// new file and write BOM bytes first
fos = new FileOutputStream(file);
byte[] bom = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF };
fos.write(bom);

// open UTF8 writer
w = new OutputStreamWriter(fos, "UTF-8");
save(w, data);
} finally {
if (w != null) try { w.close(); } catch (Exception ex) { }
if (fos != null) try { fos.close(); } catch (Exception ex) { }
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top