Byte order mark screws up file reading in Java

后端 未结 9 2493
说谎
说谎 2020-11-22 02:55

I\'m trying to read CSV files using Java. Some of the files may have a byte order mark in the beginning, but not all. When present, the byte order gets read along with the r

相关标签:
9条回答
  • 2020-11-22 03:46

    Google Data API has an UnicodeReader which automagically detects the encoding.

    You can use it instead of InputStreamReader. Here's an -slightly compactized- extract of its source which is pretty straightforward:

    public class UnicodeReader extends Reader {
        private static final int BOM_SIZE = 4;
        private final InputStreamReader reader;
    
        /**
         * Construct UnicodeReader
         * @param in Input stream.
         * @param defaultEncoding Default encoding to be used if BOM is not found,
         * or <code>null</code> to use system default encoding.
         * @throws IOException If an I/O error occurs.
         */
        public UnicodeReader(InputStream in, String defaultEncoding) throws IOException {
            byte bom[] = new byte[BOM_SIZE];
            String encoding;
            int unread;
            PushbackInputStream pushbackStream = new PushbackInputStream(in, BOM_SIZE);
            int n = pushbackStream.read(bom, 0, bom.length);
    
            // Read ahead four bytes and check for BOM marks.
            if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) {
                encoding = "UTF-8";
                unread = n - 3;
            } else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) {
                encoding = "UTF-16BE";
                unread = n - 2;
            } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) {
                encoding = "UTF-16LE";
                unread = n - 2;
            } else if ((bom[0] == (byte) 0x00) && (bom[1] == (byte) 0x00) && (bom[2] == (byte) 0xFE) && (bom[3] == (byte) 0xFF)) {
                encoding = "UTF-32BE";
                unread = n - 4;
            } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE) && (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) {
                encoding = "UTF-32LE";
                unread = n - 4;
            } else {
                encoding = defaultEncoding;
                unread = n;
            }
    
            // Unread bytes if necessary and skip BOM marks.
            if (unread > 0) {
                pushbackStream.unread(bom, (n - unread), unread);
            } else if (unread < -1) {
                pushbackStream.unread(bom, 0, 0);
            }
    
            // Use given encoding.
            if (encoding == null) {
                reader = new InputStreamReader(pushbackStream);
            } else {
                reader = new InputStreamReader(pushbackStream, encoding);
            }
        }
    
        public String getEncoding() {
            return reader.getEncoding();
        }
    
        public int read(char[] cbuf, int off, int len) throws IOException {
            return reader.read(cbuf, off, len);
        }
    
        public void close() throws IOException {
            reader.close();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:47

    The Apache Commons IO Library's BOMInputStream has already been mentioned by @rescdsk, but I did not see it mention how to get an InputStream without the BOM.

    Here's how I did it in Scala.

     import java.io._
     val file = new File(path_to_xml_file_with_BOM)
     val fileInpStream = new FileInputStream(file)   
     val bomIn = new BOMInputStream(fileInpStream, 
             false); // false means don't include BOM
    
    0 讨论(0)
  • 2020-11-22 03:49

    I had the same problem, and because I wasn't reading in a bunch of files I did a simpler solution. I think my encoding was UTF-8 because when I printed out the offending character with the help of this page: Get unicode value of a character I found that it was \ufeff. I used the code System.out.println( "\\u" + Integer.toHexString(str.charAt(0) | 0x10000).substring(1) ); to print out the offending unicode value.

    Once I had the offending unicode value, I replaced it in the first line of my file before I went on reading. The business logic of that section:

    String str = reader.readLine().trim();
    str = str.replace("\ufeff", "");
    

    This fixed my problem. Then I was able to go on processing the file with no issue. I added on trim() just in case of leading or trailing whitespace, you can do that or not, based on what your specific needs are.

    0 讨论(0)
提交回复
热议问题