IO BufferedOutputStream和BufferedInputStream

BufferedOutputStream

  FileOutputStream的子类,该类实现缓冲的输出流。通过设置这种输出流,应用程序就可以将各个字节写入基础输出流中,而不必为每次字节写入调用基础系统。

字段

  protected byte[] buf 存储数据的内部缓冲区。
  protected int count 缓冲区中的有效字节数。

构造函数

  BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的基础输出流。
  BufferedOutputStream(OutputStream out, int size) 创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的基础输出流。

方法

  void flush() 刷新此缓冲的输出流。
  void write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此缓冲的输出流。
  void write(int b) 将指定的字节写入此缓冲的输出流。

BufferedInputStream

  FileInputStream的子类,作为另一种输入流,BufferedInputStream 为添加了功能,即缓冲输入和支持 markreset 方法的能力。创建 BufferedInputStream 时即创建了一个内部缓冲区数组。读取或跳过流中的各字节时,必要时可根据所包含的输入流再次填充该内部缓冲区,一次填充多个字节。mark 操作记录输入流中的某个点,reset 操作导致在从所包含的输入流中获取新的字节前,再次读取自最后一次 mark 操作以来所读取的所有字节。

字段

  protected byte[] buf 存储数据的内部缓冲区数组。
  protected int count 比缓冲区中最后一个有效字节的索引大一的索引。
  protected int marklimit 调用 mark 方法后,在后续调用 reset 方法失败前所允许的最大提前读取量。
  protected int markpos 最后一次调用 mark 方法时 pos 字段的值。
  protected int pos 缓冲区中的当前位置。

构造函数

  BufferedInputStream(InputStream in) 创建 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
  BufferedInputStream(InputStream in, int size) 创建具有指定缓冲区大小的 BufferedInputStream,并保存其参数,即输入流 in,以便将来使用。

方法

  int available() 返回可以不受阻塞地从此输入流读取的字节数。
  void close() 关闭此输入流并释放与该流关联的所有系统资源。
  void mark(int readlimit) 参见 InputStream 的 mark 方法的常规协定。
  boolean markSupported() 测试此输入流是否支持 mark 和 reset 方法。
  int read() 参见 InputStream 的 read 方法的常规协定。
  int read(byte[] b, int off, int len) 在此字节输入流中从给定的偏移量开始将各字节读取到指定的 byte 数组中。
  void reset() 参见 InputStream 的 reset 方法的常规协定。
  long skip(long n) 参见 InputStream 的 skip 方法的常规协定。

缓冲流复制文件速度对比

public class Test {

    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        File src = new File("E:\expimp89\89.dmp");//文件大小12M
        File desc = new File("F:\89.dmp");
        //copy_1(src, desc);102412  毫秒
        //copy_2(src, desc);145  毫秒
        //copy_3(src, desc);//471  毫秒
        copy_4(src, desc);//34  毫秒
        long end = System.currentTimeMillis();
        System.out.println((end-start));
    }
    
    //单个字节复制 无缓冲流
    public static void copy_1(File src,File desc) throws IOException{
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(desc);
        int len = 0;
        while((len=fis.read())>-1){
            fos.write(len);
        }
        fos.close();
        fis.close();
    }
    
    //字节数组复制 无缓冲流
    public static void copy_2(File src,File desc) throws IOException{
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(desc);
        int len = 0;
        byte[] bytes = new byte[1024];
        while((len=fis.read(bytes))>-1){
            fos.write(bytes,0,len);
        }
        fos.close();
        fis.close();
    }
    
    //单个字节复制 缓冲流
    public static void copy_3(File src,File desc) throws IOException{
        FileInputStream fis = new FileInputStream(src);
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream(desc);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int len = 0;
        while((len=bis.read())>-1){
            bos.write(len);
        }
        bos.close();
        bis.close();
    }
    
    //字节数组复制 缓冲流
    public static void copy_4(File src,File desc) throws IOException{
        FileInputStream fis = new FileInputStream(src);
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream(desc);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int len = 0;
        byte[] bytes = new byte[1024];
        while((len=bis.read(bytes))>-1){
            bos.write(bytes,0,len);
        }
        bos.close();
        bis.close();
    }
}
原文地址:https://www.cnblogs.com/ms-grf/p/7270699.html