字符流读取,乱码问题
生活随笔
收集整理的這篇文章主要介紹了
字符流读取,乱码问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 碰到問題,字符流讀取文本文件,讀取輸出,強轉成char出現亂碼問題。
題目
用流統計文本文件的字符個數
public static int getSum() {int count = 0;BufferedReader br = null;try {// 字節流轉換為字符流, 設置編碼。br = new BufferedReader(new InputStreamReader(new FileInputStream("E:\\Txt\\2.txt"), "utf-8"));int i = 0;while ((i = br.read()) != -1) {// 13 制表, 10換行,9 tabif (i != 13 && i != 10 && i != 9) {System.out.print((char)i);count ++;}}System.out.println();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (br != null)br.close();} catch (IOException e) {e.printStackTrace();}}System.out.println(count);return count;}只要用InputStreamReader,轉換一下,就可以設置編碼了。
- 還有一種文件保存的編碼問題
- 另存為ANSI編碼
總結
以上是生活随笔為你收集整理的字符流读取,乱码问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery 属性,css,文档操作
- 下一篇: java.security.key ja