IO流之字节缓冲流

字节缓冲流构造方法

何为字节缓冲流, BufferOutputStream:该类实现缓冲输出流。通过设置这样的输出流,应用程序可以像底层输出流写入字节,而不必为写入的每个字节导致底层的系统调用。
BufferedInputStream:创建BufferedInputStream将创建一个内部缓冲区数组,当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次多个字节。

构造方法:

我们来用代码实现一下:

    public static void main(String[] args) throws Exception, UnknownHostException {
        //字节缓冲输出流 BufferedOutputStream(OutputStream out)
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\suxiaoxiao\\sisi.txt"));
        bos.write("钱塘苏小小".getBytes());
        bos.close();

        //字节缓冲输入流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\suxiaoxiao\\sisi.txt"));
       //一次读取一个字节数组数据
        byte[] bys = new byte[1024];
        int len;
        while((len = bis.read(bys))!=-1){
            System.out.println(new String(bys,0,len));
        }
        bis.close();
    }

字节缓冲流读取视屏:我们来实现下

 public static void main(String[] args) throws Exception, UnknownHostException {
         //有个需求 把D:\\suxiaoxiao\\sisi.avi这个视屏复制到C:\\suxiaoxiao目录下
        //我们来实现下
        //字节缓冲流一次读写一个字节数组
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\suxiaoxiao\\sisi.avi"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\suxiaoxiao\\sisi.avi"));
        byte[] bys = new byte[1024];
        int by;
        while ((by=bis.read(bys))!=-1) {
            bos.write(by);
        }
     bis.close();
     bos.close();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值