java: how to convert a file to utf8

后端 未结 4 1051
一向
一向 2020-12-09 11:42

i have a file that have some non-utf8 caracters (like \"ISO-8859-1\"), and so i want to convert that file (or read) to UTF8 encoding, how i can do it?

The code it\'s

4条回答
  •  我在风中等你
    2020-12-09 12:04

      String charset = "ISO-8859-1"; // or what corresponds
      BufferedReader in = new BufferedReader( 
          new InputStreamReader (new FileInputStream(file), charset));
      String line;
      while( (line = in.readLine()) != null) { 
        ....
      }
    

    There you have the text decoded. You can write it, by the simmetric Writer/OutputStream methods, with the encoding you prefer (eg UTF-8).

提交回复
热议问题