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


I have a file that is encoded as ISO-8859-1, and contains characters such as o.

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

  file = new file ("myfile.csv"); InputStream fr = new FileInputStream (in); Byte [] buffer = new byte [4096]; Whereas (true) {int byteCount = fr.read (buffer, 0, buffer lamps); If (Bytecount & lt; = 0) {break; } String s = new string (buffer, 0, bytecount, "ISO-885 9-1"); Println (s); }  

Although the รด character is always distorted, usually prints as one? .

I have read this topic around (and learned a bit on the way) eg.

  • But still this can not work

    Interestingly, this is my local PC (XP) Works but not on my Linux box

    I have checked that using my JDK supports the necessary charsets (they are standard, so no wonder):

      println ( Java.nio.charset.Charset.availableCharsets ());  

    I doubt that your file is actually Is encoded as iOS-8859-1, or system.out is not the way to print the character.

    I recommend that you check the related bytes in the file to check for the first. To examine the second, check the corresponding character in the string, print it

      with system.out.println ((int) s.getCharAt (index));  

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

    Look for general advice (the submission code is in C #, but converting to Java is easy, and the principles are the same).

    In general, by the way, I wrap the stream with an InputStreamReader with the right encoding - it is easier than creating new strings "by hand" I know that this Only the demo code can be.

    Edit: Here's an easy way to prove whether the console will work or not:

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

    Comments