IO—》转换流和缓冲流

转换流

摘要:

InputStreamReader和OutputStreamWriter他们分别是FileReader和FileWriter的父类

当只是单纯的读写文件而不改变文件的编码格式时,就分别用他们的子类就可以

而当要改变编码格式不用系统默认编码格式(GBK)时,就需要用他们的父类来处理,父类提供了改变编码格式的方法。

OutputStreamWriter类

OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字节。

它的作用的就是,将字符串按照指定的编码表转成字节,在使用字节流将这些字节写出去。

public static void main(String[] args) throws IOException {
    FileOutputStream fos=new FileOutputStream("e:\test\utf8.txt",true);
    OutputStreamWriter osw=new OutputStreamWriter(fos,"utf-8");
    osw.write("这是utf-8编码haha");
    osw.close();
}

当我们调用OutputStreamWriter对象的write方法时,会拿着字符到指定的码表中进行查询,把查到的字符编码值转成字节数存放到OutputStreamWriter缓冲区中。

然后再调用刷新功能,或者关闭流,或者缓冲区存满后会把缓冲区中的字节数据使用字节流写到指定的文件中

InputStreamReader

public static void main(String[] args) throws IOException {
    //明确数据源
    FileInputStream fis=new FileInputStream("e:\test\utf8.txt");
    InputStreamReader isr=new InputStreamReader(fis,"utf-8");
    char[] ch=new char[1024];
    int len=0;
    while((len=isr.read(ch))!=-1){
        System.out.println(new String(ch,0,len));
    }
    isr.close();
}

总结:

字节--->字符 : 看不懂的--->看的懂的。  需要读。输入流。 InputStreamReader

字符--->字节 : 看的懂的--->看不懂的。  需要写。输出流。 OutputStreamWriter

缓冲流

字节缓冲流

①写入数据到流中,字节缓冲输出流 BufferedOutputStream

②读取流中的数据,字节缓冲输入流 BufferedInputStream

它们的内部都包含了一个缓冲区,通过缓冲区读写,就可以提高了IO流的读写速度

字节缓冲输出流BufferedOutputStream

构造方法:

public BufferedOutputStream(OutputStream out)创建一个新的缓冲输出流,以将数据写入指定的底层输出流。

//缓冲字节输出流
    public static void method1() throws IOException{
        FileOutputStream fos=new FileOutputStream("e:\test\haha",true);
        BufferedOutputStream bos=new BufferedOutputStream(fos);
        //bos.write(100);
        bos.write("你好啊".getBytes());
        bos.close();
    }

字节缓冲输入流 BufferedInputStream

构造方法:

public BufferedInputStream(InputStream in)

//缓冲字节输入流
    public static void method2() throws IOException{
        FileInputStream fis=new FileInputStream("e:\test\haha");
        BufferedInputStream bis=new BufferedInputStream(fis);
        int len=0;
        while((len=bis.read())!=-1){
            System.out.print((char)len);
        }
        bis.close();
    }

实例:

对比字节流read(),字节流read(byte []),缓冲字节流read(),缓冲字节流read(byte [])它们的速度

//字节流read()
    public static void method1() throws IOException{
        long time1=System.currentTimeMillis();
        FileInputStream fis=new FileInputStream("d:\DW\jdk\javafx-src.zip");
        FileOutputStream fos=new FileOutputStream("e:\test\text\haha1.zip");
        int len=0;
        while((len=fis.read())!=-1){
            fos.write(len);
        }
        long time2=System.currentTimeMillis();
        System.out.println(time2-time1);
        fis.close();
        fos.close();
    }
    //字节流read(byte [])
    public static void method2() throws IOException{
        long time1=System.currentTimeMillis();
        FileInputStream fis=new FileInputStream("d:\DW\jdk\javafx-src.zip");
        FileOutputStream fos=new FileOutputStream("e:\test\text\haha2.zip");
        int len=0;
        byte[] bytes=new byte[1024];
        while((len=fis.read(bytes))!=-1){
            fos.write(bytes, 0, len);
        }
        long time2=System.currentTimeMillis();
        System.out.println(time2-time1);
        fis.close();
        fos.close();
    }
    //缓冲字节流read()
    public static void method3() throws IOException{
        long time1=System.currentTimeMillis();
        FileInputStream fis=new FileInputStream("d:\DW\jdk\javafx-src.zip");
        BufferedInputStream bis=new BufferedInputStream(fis);
        FileOutputStream fos=new FileOutputStream("e:\test\text\haha3.zip");
        BufferedOutputStream bos=new BufferedOutputStream(fos);
        int len=0;
        while((len=bis.read())!=-1){
            bos.write(len);
        }
        long time2=System.currentTimeMillis();
        System.out.println(time2-time1);
        fis.close();
        fos.close();
    }
    //缓冲字节流read(byte [])
    public static void method4() throws IOException{
        long time1=System.currentTimeMillis();
        FileInputStream fis=new FileInputStream("d:\DW\jdk\javafx-src.zip");
        BufferedInputStream bis=new BufferedInputStream(fis);
        FileOutputStream fos=new FileOutputStream("e:\test\text\haha4.zip");
        BufferedOutputStream bos=new BufferedOutputStream(fos);
        int len=0;
        byte[] bytes=new byte[1024];
        while((len=bis.read(bytes))!=-1){
            bos.write(bytes, 0, len);
        }
        long time2=System.currentTimeMillis();
        System.out.println(time2-time1);
        fis.close();
        fos.close();
    }

结果:

文件大小:4m

字节流read() VS 字节流read(byte [])
150296毫秒 200毫秒
字节流read(byte [])完胜

缓冲字节流read() VS缓冲字节流read(byte [])
410毫秒 60毫秒
缓冲字节流read(byte [])完胜

速度总结:(慢——>快)

字节流read(),缓冲字节流read() ,字节流read(byte []),缓冲字节流read(byte [])

原文地址:https://www.cnblogs.com/Ace-suiyuan008/p/9516800.html