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

Discussion in 'Java' started by X_AWieminer_X, Jul 15, 2003.

  1. 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) { }
    }
    }
     
    X_AWieminer_X, Jul 15, 2003
    #1
    1. Advertising

  2. X_AWieminer_X

    John Salvo Guest

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


    I don't think that is true ... since I was working with UTF-8 encoded
    files with the BOM on front.

    What JVM and version are you using ?
     
    John Salvo, Jul 16, 2003
    #2
    1. Advertising

  3. X_AWieminer_X

    JomoFrodo

    Joined:
    Aug 18, 2011
    Messages:
    1
    Actually, your solution seems pretty good to me. Thanks!
     
    JomoFrodo, Aug 18, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jean-Marc Autexier
    Replies:
    2
    Views:
    3,780
    Jean-Marc Autexier
    Aug 30, 2003
  2. Achim Domma
    Replies:
    5
    Views:
    13,880
    skfh82
    May 12, 2007
  3. Replies:
    5
    Views:
    410
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Dec 23, 2005
  4. Chris Morris

    Output UTF-16LE BOM to file - 1.9

    Chris Morris, Apr 9, 2009, in forum: Ruby
    Replies:
    1
    Views:
    172
    James Gray
    Apr 12, 2009
  5. Replies:
    2
    Views:
    410
    Nathan Keel
    Aug 14, 2009
Loading...

Share This Page