java io流字符乱码问题

一个中文占两个字节,在字符串中(一个中文占3个字节(utf-8)或者2个字节(GBK)),所以用字节流读入 写出中文时会出现乱码的情况。

解决办法呢,就是将读入的字节流转换成字符串形式,这种方式不一定能够能解决成功

先演示读出问题:test/test0.txt的内容如下:

hello世界 世界

读取test0.txt文件

package IoCharctorTest01;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Test01 {
    public static void main(String[] args){
        try (
                FileInputStream fis=new FileInputStream("test//test0.txt");
                ){
            int temp;
            String str="";
            byte [] bytes= new byte[3];
            while ((temp=fis.read(bytes))!=-1){
                System.out.println(new String(bytes,0,temp));
                //下面这种方式是通过字符串拼接,将byte数组转换成字符串,跟上面直接转成字符串效果无差别
                str =str +new String(bytes,0,temp);
               System.out.println(str );
               str="";
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
out:
hel
hel
lo�
lo�
���
���
�� 
�� 
世
世
界
界

为什么会出现乱码,分析,文本文件中的中文字符是以两个字节的形式存储,咱们设置byte数组的时候设置的长度为3,在文件中汉字占用的是第6,7,8,9,,11,12,13,14这几个字节,那byte数组,第3次读取的时候正好读到第6个字节,读了中文字符的一个字节,所以显示不出来,第3次读取的时候读了7,8,9,其中7是"世"的第二个字节,8,9是界的两个字节,显然这次也会乱码,第4次读取的时候,读了10,11,12,其中10为空格,11,12是"世"的两个字节,这次可以正常转换为字符串,第5次读取读出13,14,这两个字节是"界"的两个字节,显然也可以正常显示,所以这种方式是有风险的,因为如果中文字符的字节正好处于3,4时,必然乱码

演示写入问题:

上面已经说过字符串中的汉字UTF-8编码占3个字节,GBK编码占2个字节

package IoCharctorTest01;



import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class Test03 {

    public static void main(String[] args) {
        try (FileOutputStream fis = new FileOutputStream("test\\test3.txt");) {
            String msg = "你好世界";

            //每次写出4个字节,因为一个中文(utf-8)占用3个字节,所以导致乱码
            fis.write(msg.getBytes(), 0, 4);
            //换行
            fis.write("\n".getBytes());
            //如果byte数组正好能写入对应于汉字的整的字节数,不会出现乱码
            fis.write("你好世界".getBytes(),0,6);
            fis.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
out:
test3.txt内容为:
你�
你好

直接读直接写

以下代码经过测试,这种直接读直接写没有问题 ,不会出现汉字乱码情况,即便每次读一个字节写一个字节也不会出现乱码

package IoCharctorTest01;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test02 {
    public static void main(String[] args){
        try (
                FileInputStream fis=new FileInputStream("test//test0.txt");
                FileOutputStream fos=new FileOutputStream("test//test2.txt");
        ){
            int temp;
            byte [] bytes= new byte[5];
            while ((temp=fis.read(bytes))!=-1){
                fos.write(bytes,0,temp);

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

解决办法:

用字符流解决读写问题,下篇博客解决

猜你喜欢

转载自blog.csdn.net/sinat_41132860/article/details/84337599