Search code examples
javabyte-order-mark

BOM in Windows and Unix


I write new file with lines and need utf-8 with BOM. In my code I add BOM with the simplest way:

printStream.print('\ufeff');
// print lines

And in Win7 this method work fine, but when I execute my jar in Unix I see "?" at prolog and "utf-8 without bom encoding", how can I fix it? Crossplatform prefered... ANSWER:

char[] c = {0xEF, 0xBB, 0xBF};
        for(int i=0; i<3; i++){
            printStream.write(c[i]);
        }

Solution

  • I'm not sure printStream.print('\ufeff'); is the correct way of writing the utf-8 bom. You could try and write the bytes EF BB BF directly.

    Example:

    char[] bom = { 0xEF, 0xBB, 0xBF }; 
    //or byte[] bom= { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };
    printStream.write( bom ); //directly write the bytes