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

后端 未结 5 871
一个人的身影
一个人的身影 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 04:45

    I suspect that either your file isn't actually encoded as ISO-8859-1, or System.out doesn't know how to print the character.

    I recommend that to check for the first, you examine the relevant byte in the file. To check for the second, examine the relevant character in the string, printing it out with

     System.out.println((int) s.getCharAt(index));
    

    In both cases the result should be 244 decimal; 0xf4 hex.

    See my article on Unicode debugging for general advice (the code presented is in C#, but it's easy to convert to Java, and the principles are the same).

    In general, by the way, I'd wrap the stream with an InputStreamReader with the right encoding - it's easier than creating new strings "by hand". I realise this may just be demo code though.

    EDIT: Here's a really easy way to prove whether or not the console will work:

     System.out.println("Here's the character: \u00f4");
    

提交回复
热议问题