Java FileInputStream讀中文亂碼問題解決方案
1、前提
以讀取編碼是GBK的文件為案例,文件內容只有中文和中文符號
2、原因
FileInputStream讀中文亂碼是因為一個中文對應兩個字節存儲(負數),也就是說,讀取對應中文的字節數應該是偶數; 而英文對應一個字節存儲。FileInputStream每次讀取一個數組長度的字節時,讀取的中文字節數可能是奇數,也就是只讀到中文的一半字節,出現亂碼。
3、解決方法
一次讀取所有字節,此方法不靠譜,因為不確定總字節數。
在輸出時進行判斷,遍歷數組判斷負數的個數,如果是奇數,說明讀取到中文的一半字節,對數組進行擴容再輸出;否則正常輸出
4、代碼案例
package 第二題;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.Arrays;public class MainTest { public static void main(String[] args) throws UnsupportedEncodingException { // 創建File對象 File file = new File('D:filetestfile4.txt'); FileInputStream fileInputStream = null; try { // 新建一個FileInputStream對象 fileInputStream = new FileInputStream(file); // 新建一個字節數組 byte[] buf = new byte[2]; // read(buf):此方法的返回值就是當前讀取的字節個數,將數據讀取到buf數組 // 將readLen變量也就是read方法的返回值,當此變量等于-1,則讀到文件末尾 int readLen = -1; //讀取文件數據 while ((readLen = fileInputStream.read(buf)) != -1) { int pos=0;//記錄負數的個數for(byte v:buf){ if(v<0) { pos++; }}//負數個數為偶數,讀取完整,沒有讀取到半個中文if(pos%2==0){ // 將字節數組轉換成字符串 String content = new String(buf, 0, readLen); System.out.print(content);}else {//負數個數為奇數,讀取不完整,會亂碼 //再讀取下一位字節 int nextByteValue=fileInputStream.read(); int nextLen=readLen+1; //字節數組擴容一位 buf= Arrays.copyOf(buf,nextLen); buf[readLen]= (byte) nextByteValue; String content=new String(buf,0,nextLen); System.out.print(content); //奇數,字節補全 //針對數組擴容一個字節單元/* buf=Arrays.copyOf(buf, readLen+1); int nextByteValue=fileInputStream.read(); buf[readLen]= (byte) nextByteValue; String content = new String(buf, 0, readLen); System.out.print(content);*/} } } catch (FileNotFoundException e) { // 輸出堆棧信息 e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try {// 文件輸入流關閉(釋放資源)fileInputStream.close(); } catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace(); } } }}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. php網絡安全中命令執行漏洞的產生及本質探究2. 三個不常見的 HTML5 實用新特性簡介3. Angular獲取ngIf渲染的Dom元素示例4. IIS+PHP添加對webp格式圖像的支持配置方法5. ASP調用WebService轉化成JSON數據,附json.min.asp6. 無線標記語言(WML)基礎之WMLScript 基礎第1/2頁7. 使用.net core 自帶DI框架實現延遲加載功能8. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析9. php測試程序運行速度和頁面執行速度的代碼10. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執行過程解析
