博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IO BufferedOutputStream和BufferedInputStream
阅读量:6967 次
发布时间:2019-06-27

本文共 3835 字,大约阅读时间需要 12 分钟。

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

你可能感兴趣的文章
Telnet 工具远程连接服务器
查看>>
发布一个WinForm控件--TableViewControl
查看>>
CSDN 四川大学线下编程比赛第一题:数字填充
查看>>
RC4加密算法的原理及实现
查看>>
SignalR
查看>>
片段·刺杀
查看>>
什么是CMS
查看>>
最长公共子序列
查看>>
数据分析系列剧第二集:七步法的应用
查看>>
[LeetCode] Island Perimeter 岛屿周长
查看>>
创建Android本地repo
查看>>
php接收二进制文件转换成图片
查看>>
socket 之TIME_WAIT状态
查看>>
wchar_t*和char*之间的互相转换的那些事
查看>>
C# WinForm 应用程序 开启Console窗口
查看>>
基于MVC4+EasyUI的Web开发框架经验总结(2)- 使用EasyUI的树控件构建Web界面
查看>>
jquery,禁止冒泡和默认行为
查看>>
WF4.0实战(一):文件审批流程
查看>>
【java】java处理随机浮点数(小数点后两位)用RMB的大写数值规则输出
查看>>
检测不再使用的索引--check-unused-keys
查看>>