java基础:字节缓冲流BufferedInputStream和BufferedOutputStream

来源:互联网 发布:书法边框软件 编辑:程序博客网 时间:2024/05/10 23:09

    BufferedInputStreamBufferedOutputStream这两个流类为IO提供了带缓冲区的操作,一般打开文件,进行写入或读取操作时,都会加上缓冲。这种流模式,提高了IO的性能。
    从应用程序中把数据放入文件,就相当于把一缸水倒入到另一缸水中。
     FileOutputStream当中的write()方法相当于把水一滴一滴传过去
     DataOutputStream当中的writeXxx()方法(如writeInt()等方法)会方便一些,相当于一瓢一瓢转移
     BufferedOutputStream当中的writeXxx()方法更方便,相当于一瓢一瓢水先放入桶中,再从桶中倒入到另一个缸中
    这里我们来测试一下文件拷贝过程中,单字节拷贝,利用字节数组批量拷贝和利用字节缓冲流进行拷贝哪一个效率更高。

public void copyFileByBuffer(File src, File dest) throws IOException {        // 进行文件的拷贝,利用带缓冲的字节流        if (!src.exists()) {            throw new IllegalArgumentException("文件不存在");// 参数异常        }        if (!src.isFile()) {            throw new IllegalArgumentException("不是文件!");        }        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));        int c;        while ((c = bis.read()) != -1) {            bos.write(c);            bos.flush();// 刷新缓冲区,一定要写,否则写入不到文件中去        }        bis.close();        bos.close();    }    public void CopyFile(File src,File dest) throws Exception{    //利用字节数组进行批量拷贝    if(!src.exists()) {        throw new IllegalArgumentException("文件不存在");//参数异常    }    if(!src.isFile()) {        throw new IllegalArgumentException("不是文件!");    }    FileInputStream in=new FileInputStream(src);    FileOutputStream out=new FileOutputStream(dest);    byte[] buf=new byte[8*1024];    int b;    while((b=in.read(buf,0,buf.length))!=-1) {        out.write(buf,0, b);        out.flush();    }    in.close();    out.close();}public void copyFileBybyte(File src,File dest) throws IOException {    //单字节拷贝    if (!src.exists()) {        throw new IllegalArgumentException("文件不存在");// 参数异常    }    if (!src.isFile()) {        throw new IllegalArgumentException("不是文件!");    }    FileInputStream in=new FileInputStream(src);    FileOutputStream out=new FileOutputStream(dest);    int c;    while((c=in.read())!=-1) {        out.write(c);        out.flush();    }    in.close();    out.close();}

注意:在利用字节缓冲流时,每进行一次写入,一定要刷新缓冲区,否则无法将内容写入到文件中

进行测试:

public static void main(String[] args) throws Exception {        // TODO Auto-generated method stub        long start=System.currentTimeMillis();        Buffered buffer = new Buffered();        buffer.copyFileByBuffer(new File("D:\\log_network.txt"), new File("D:\\buffer.txt"));        long end1=System.currentTimeMillis();        buffer.CopyFile(new File("D:\\log_network.txt"), new File("D:\\bytes.txt"));        long end2=System.currentTimeMillis();        buffer.copyFileBybyte(new File("D:\\log_network.txt"), new File("D:\\byte.txt"));        long end3=System.currentTimeMillis();        System.out.println("带缓冲的文件拷贝时间为"+(end1-start));        System.out.println("批量拷贝文件的时间为:"+(end2-end1));        System.out.println("单字节的文件拷贝的时间为:"+(end3-end2));    }

测试结果:

带缓冲的文件拷贝时间为125
批量拷贝文件的时间为:0
单字节的文件拷贝的时间为:143

观察测试结果发现,运行时间最少的为批量拷贝,由于文件较小,运行时间十分短,而带缓冲的文件拷贝时间为125毫秒,小于单字节的文件拷贝时间

阅读全文
0 0
原创粉丝点击