Java App : Unable to read iso-8859-1 encoded file correctly

后端 未结 5 876
一个人的身影
一个人的身影 2020-12-16 04:27

I have a file which is encoded as iso-8859-1, and contains characters such as ô .

I am reading this file with java code, something like:

File in = n         


        
5条回答
  •  醉酒成梦
    2020-12-16 05:01

    @Joel - your own answer confirms that the problem is a difference between the default encoding on your operating system (UTF-8, the one Java has picked up) and the encoding your terminal is using (ISO-8859-1).

    Consider this code:

    public static void main(String[] args) throws IOException {
        byte[] data = { (byte) 0xF4 };
        String decoded = new String(data, "ISO-8859-1");
        if (!"\u00f4".equals(decoded)) {
            throw new IllegalStateException();
        }
    
        // write default charset
        System.out.println(Charset.defaultCharset());
    
        // dump bytes to stdout
        System.out.write(data);
    
        // will encode to default charset when converting to bytes
        System.out.println(decoded);
    }
    

    By default, my Ubuntu (8.04) terminal uses the UTF-8 encoding. With this encoding, this is printed:

    UTF-8

    If I switch the terminal's encoding to ISO 8859-1, this is printed:

    UTF-8
    ôô

    In both cases, the same bytes are being emitted by the Java program:

    5554 462d 380a f4c3 b40a
    

    The only difference is in how the terminal is interpreting the bytes it receives. In ISO 8859-1, ô is encoded as 0xF4. In UTF-8, ô is encoded as 0xC3B4. The other characters are common to both encodings.

提交回复
热议问题