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) { }
}
}
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) { }
}
}