Reading UTF-8 from a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Good old Grim
    New Member
    • Feb 2008
    • 7

    #1

    Reading UTF-8 from a file

    I'm currently reading UTF-8 encoded data from a file like this:

    Code:
    BufferedReader input =  new BufferedReader(new FileReader(new File(System.getProperty("java.library.path")+"\\"+_file)));
    String curline = new String(input.readLine().getBytes(),"UTF-8");
    This works fine for most special characters, but two characters ('ā' and 'š') do not seem to be recognized (all other special characters from my language are recognized properly: ēŗūīōģļķžčņ ). When I output the strings in GUI, these two characters are output as a square and a '?' (each character as a combination of both of these).

    If I pass the same characters as literals, they are displayed properly.

    All files (including the source with literals and the file being read) are encoded in UTF-8 and edited using Notepad2 (not a typo - there is a program called Notepad2).

    The code is compiled using the -encoding UTF-8 parameter.

    So - how do I get proper UTF-8 from a file all the way to GUI?
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Originally posted by Good old Grim
    I'm currently reading UTF-8 encoded data from a file like this:

    [code=Java]BufferedReader input = new BufferedReader( new FileReader(new File(System.get Property("java. library.path")+ "\\"+_file) ));
    String curline = new String(input.re adLine().getByt es(),"UTF-8");
    [/code]
    Nice try, but your use of "UTF-8" is closing the barn door after the horse has escaped. You need to indicate the encoding at the stage where Java is converting from bytes to characters. Unfortunately, that is in FileReader. FileReader is a somewhat useless subclass of InputStreamRead er; "somewhat useless" in that it doesn't provide a constructor that takes charset name. Solution: use the real deal, InputStreamRead er:

    [CODE=Java]String path = ...
    BufferedReader reader =
    new BufferedReader(
    new InputStreamRead er(
    new FileInputStream (path), "UTF-8"));[/CODE]

    Comment

    Working...