zoukankan      html  css  js  c++  java
  • 101.Java中IO流_字节缓冲流

    1.1.   缓冲流

    上述程序中我们为了提高流的使用效率,自定义了字节数组,作为缓冲区.Java其实提供了专门的字节流缓冲来提高效率.

    BufferedInputStream和BufferedOutputStream

    BufferedOutputStream和BufferedOutputStream类可以通过减少读写次数来提高输入和输出的速度。它们内部有一个缓冲区,用来提高处理效率。查看API文档,发现可以指定缓冲区的大小。其实内部也是封装了字节数组。没有指定缓冲区大小,默认的字节是8192。

    显然缓冲区输入流和缓冲区输出流要配合使用。首先缓冲区输入流会将读取到的数据读入缓冲区,当缓冲区满时,或者调用flush方法,缓冲输出流会将数据写出。

    注意:当然使用缓冲流来进行提高效率时,对于小文件可能看不到性能的提升。但是文件稍微大一些的话,就可以看到实质的性能提升了。

    public class IoTest5 {
        public static void main(String[] args) throws IOException {
            String srcPath = "c:\a.mp3";
            String destPath = "d:\copy.mp3";
            copyFile(srcPath, destPath);
        }
    
        public static void copyFile(String srcPath, String destPath)
                throws IOException {
            // 打开输入流,输出流
            FileInputStream fis = new FileInputStream(srcPath);
            FileOutputStream fos = new FileOutputStream(destPath);
    
            // 使用缓冲流
            BufferedInputStream bis = new BufferedInputStream(fis);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
    
            // 读取和写入信息
            int len = 0;
    
            while ((len = bis.read()) != -1) {
                bos.write(len);
            }
    
            // 关闭流
            bis.close();
            bos.close();    
    }
    
    }
    author@nohert
  • 相关阅读:
    Tensorflow实现LSTM识别MINIST
    linux误删除恢复
    python使用工具简介介绍
    一个画ROC曲线的封装包
    Anaconda基本使用
    对于进程没杀死占用内存和cpu行为的方法
    Gluon
    原博客地址
    训练词向量
    TPU尝试
  • 原文地址:https://www.cnblogs.com/gzgBlog/p/13624609.html
Copyright © 2011-2022 走看看