Java FileInputStream讀中文亂碼問題解決方案
1、前提
以讀取編碼是GBK的文件為案例,文件內(nèi)容只有中文和中文符號
2、原因
FileInputStream讀中文亂碼是因?yàn)橐粋€(gè)中文對應(yīng)兩個(gè)字節(jié)存儲(負(fù)數(shù)),也就是說,讀取對應(yīng)中文的字節(jié)數(shù)應(yīng)該是偶數(shù); 而英文對應(yīng)一個(gè)字節(jié)存儲。FileInputStream每次讀取一個(gè)數(shù)組長度的字節(jié)時(shí),讀取的中文字節(jié)數(shù)可能是奇數(shù),也就是只讀到中文的一半字節(jié),出現(xiàn)亂碼。
3、解決方法
一次讀取所有字節(jié),此方法不靠譜,因?yàn)椴淮_定總字節(jié)數(shù)。
在輸出時(shí)進(jìn)行判斷,遍歷數(shù)組判斷負(fù)數(shù)的個(gè)數(shù),如果是奇數(shù),說明讀取到中文的一半字節(jié),對數(shù)組進(jìn)行擴(kuò)容再輸出;否則正常輸出
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 { // 創(chuàng)建File對象 File file = new File('D:filetestfile4.txt'); FileInputStream fileInputStream = null; try { // 新建一個(gè)FileInputStream對象 fileInputStream = new FileInputStream(file); // 新建一個(gè)字節(jié)數(shù)組 byte[] buf = new byte[2]; // read(buf):此方法的返回值就是當(dāng)前讀取的字節(jié)個(gè)數(shù),將數(shù)據(jù)讀取到buf數(shù)組 // 將readLen變量也就是read方法的返回值,當(dāng)此變量等于-1,則讀到文件末尾 int readLen = -1; //讀取文件數(shù)據(jù) while ((readLen = fileInputStream.read(buf)) != -1) { int pos=0;//記錄負(fù)數(shù)的個(gè)數(shù)for(byte v:buf){ if(v<0) { pos++; }}//負(fù)數(shù)個(gè)數(shù)為偶數(shù),讀取完整,沒有讀取到半個(gè)中文if(pos%2==0){ // 將字節(jié)數(shù)組轉(zhuǎn)換成字符串 String content = new String(buf, 0, readLen); System.out.print(content);}else {//負(fù)數(shù)個(gè)數(shù)為奇數(shù),讀取不完整,會(huì)亂碼 //再讀取下一位字節(jié) int nextByteValue=fileInputStream.read(); int nextLen=readLen+1; //字節(jié)數(shù)組擴(kuò)容一位 buf= Arrays.copyOf(buf,nextLen); buf[readLen]= (byte) nextByteValue; String content=new String(buf,0,nextLen); System.out.print(content); //奇數(shù),字節(jié)補(bǔ)全 //針對數(shù)組擴(kuò)容一個(gè)字節(jié)單元/* 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 {// 文件輸入流關(guān)閉(釋放資源)fileInputStream.close(); } catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace(); } } }}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python如何換行輸出2. Python使用urlretrieve實(shí)現(xiàn)直接遠(yuǎn)程下載圖片的示例代碼3. python如何計(jì)算圓的面積4. Android Studio中一套代碼多渠道打包的實(shí)現(xiàn)方法5. Java 接口和抽象類的區(qū)別詳解6. Java使用Tesseract-Ocr識別數(shù)字7. Python:UserWarning:此模式具有匹配組。要實(shí)際獲得組,請使用str.extract8. Android打包篇:Android Studio將代碼打包成jar包教程9. 詳解java google Thumbnails 圖片處理10. 解決Android Studio 格式化 Format代碼快捷鍵問題
