java 记事本读取文本文件的乱码问题,终于解决了 O(∩_∩)O~ 多谢 uu老师帮助

来源:互联网 发布:淘宝烽火体育是正品 编辑:程序博客网 时间:2024/05/17 03:10

void readFile(String path) // 显示打开文件的内容
 {
  try
  {
   File file = new File(path);
   BufferedReader br = new BufferedReader(new InputStreamReader(
     new FileInputStream(file), "gb2312"));// 编码转换(关键的地方)

   String str = "";
   int size = (int) file.length();
   int charRead = 0;
   char[] content = new char[size];


   while (br.ready())
   {
    charRead += br.read(content, charRead, size - charRead);
   }
   br.close();
   str = new String(content, 0, charRead);
   text.setText(str);
  } catch (IOException e)
  {
   System.out.println("读取文件出错");
  }
}

原创粉丝点击