Java FileInputStream读中文乱码问题解决方案
1、前提
以读取编码是GBK的文件为案例,文件内容只有中文和中文符号
2、原因
FileInputStream读中文乱码是因为一个中文对应两个字节存储(负数),也就是说,读取对应中文的字节数应该是偶数;而英文对应一个字节存储。FileInputStream每次读取一个数组长度的字节时,读取的中文字节数可能是奇数,也就是只读到中文的一半字节,出现乱码。
3、解决方法
一次读取所有字节,此方法不靠谱,因为不确定总字节数。
在输出时进行判断,遍历数组判断负数的个数,如果是奇数,说明读取到中文的一半字节,对数组进行扩容再输出;否则正常输出
4、代码案例
package第二题; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.IOException; importjava.io.UnsupportedEncodingException; importjava.util.Arrays; publicclassMainTest{ publicstaticvoidmain(String[]args)throwsUnsupportedEncodingException{ //创建File对象 Filefile=newFile("D:\\filetest\\file4.txt"); FileInputStreamfileInputStream=null; try{ //新建一个FileInputStream对象 fileInputStream=newFileInputStream(file); //新建一个字节数组 byte[]buf=newbyte[2]; //read(buf):此方法的返回值就是当前读取的字节个数,将数据读取到buf数组 //将readLen变量也就是read方法的返回值,当此变量等于-1,则读到文件末尾 intreadLen=-1; //读取文件数据 while((readLen=fileInputStream.read(buf))!=-1){ intpos=0;//记录负数的个数 for(bytev:buf) { if(v<0) { pos++; } } //负数个数为偶数,读取完整,没有读取到半个中文 if(pos%2==0) { //将字节数组转换成字符串 Stringcontent=newString(buf,0,readLen); System.out.print(content); }else{//负数个数为奇数,读取不完整,会乱码 //再读取下一位字节 intnextByteValue=fileInputStream.read(); intnextLen=readLen+1; //字节数组扩容一位 buf=Arrays.copyOf(buf,nextLen); buf[readLen]=(byte)nextByteValue; Stringcontent=newString(buf,0,nextLen); System.out.print(content); //奇数,字节补全 //针对数组扩容一个字节单元 /*buf=Arrays.copyOf(buf,readLen+1); intnextByteValue=fileInputStream.read(); buf[readLen]=(byte)nextByteValue; Stringcontent=newString(buf,0,readLen); System.out.print(content);*/ } } }catch(FileNotFoundExceptione){ //输出堆栈信息 e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }finally{ try{ //文件输入流关闭(释放资源) fileInputStream.close(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。