我们需要将 StandardCharsets.UTF_8
传递给 InputStreamReader
构造函数以从 UTF-8 编码文件中读取数据。
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; public class ReadUTF8Data { public static void main(String[] args) { try { File fileDir = new File("c:\temp\test.txt"); BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream(fileDir), "UTF8")); String str; while ((str = in.readLine()) != null) { System.out.println(str); } in.close(); } catch (UnsupportedEncodingException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } catch (Exception e) { System.out.println(e.getMessage()); } } }
日期:2020-09-17 00:09:25 来源:oir作者:oir