IO-字节缓冲流

字节缓冲流的作用是,在应用程序写入的时候,可以一次性写入,而不需要为写入的每个字节都调用底层系统;

BufferedInputStream:创建bufferedinputstream将创建一个内部缓冲区数组,当从流中读取或者跳过字节时,内部缓冲区

将根据需要从所包含的输入流中国重新填充,一次多个字节;

BufferedoutputStream:输出缓冲流

new  BufferedoutputStream(OutputStream out)

new  BufferedInputStream(InputStream out)

字节缓冲流仅仅提供缓冲区,真正的读写数据还是需要依靠基本的字节流对象进行操作;

字节缓冲流使用方法---包含输入输出流;

public class bufferoutputstream {
    public static void main(String[] args) throws IOException {
        //创建输出流
        FileOutputStream fos = new FileOutputStream("java.txt");
        //创建字节缓冲输出流对象
        BufferedOutputStream bos = new BufferedOutputStream(fos );
        //写数据  ,方法与FileOutputStream一致,也是三种方法

        bos.write("hello".getBytes());
        bos.close();


        //缓冲输出流,与输入流类似
        FileInputStream fis = new FileInputStream("java.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        //一次读取一个字节
        /*int by;
        while((by = bis.read())!=-1){
            System.out.println((char) by);
        }bis.close();*/

        // y一次读取一个数组
        byte[] bys = new byte[1024];
        int len;
        while ((len = bis.read(bys)) !=-1){
            System.out.print(new String(bys,0,len) );
        }
        bis.close();

    }
}

复制视频;将视频文件从一个目录下复制到另一个目录下

第四中方法是最快的;

package file;

import java.io.*;

public class VedioCopy {
    public static void main(String[] args) throws IOException {
        //记录开始时间
        long starttime = System.currentTimeMillis();
        method1();
        //记录结束时间
        long endtime = System.currentTimeMillis();
        long totaltime = endtime-starttime;
        System.out.println(totaltime);

    }

    //复制视频

    public static void method1() throws IOException {

        FileInputStream fis = new FileInputStream("a.avi");
        FileOutputStream fos = new FileOutputStream(".\\collectors\\b.avi");

        //一次读写一个字节
        int by;
        while ((by =fis.read()) != -1){
            fos.write(by);
            }
        fis.close();
        fos.close();
    }

    //基本字节流一次读取一个字节数组
    public  static void method2 () throws IOException {
        FileInputStream fis = new FileInputStream("ab.avi");
        FileOutputStream fos = new FileOutputStream(".\\collections\\java.avi");

        byte[] by = new byte[1024];
        int len;
        while ((len = fis.read(by))!= -1){
            fos.write(by,0,len);
        }fis.close();
        fos.close();
    }

    //利用字节缓冲流一次读取一个字节


    //利用字节缓冲流一次读取一个数组

    public  static  void method4() throws IOException {
        BufferedInputStream bif = new BufferedInputStream(new FileInputStream("filename"));
        BufferedOutputStream bof = new BufferedOutputStream(new FileOutputStream("filenam"));

        byte[] bys = new byte[1024];
        int len ;
         while((len = bif.read(bys)) != -1){
             bof.write(bys,0,len);
         }bif.close();bof.close();

    }


}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zero _s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值