使用缓冲流实现复制视频、图片

使用缓冲流实现复制视频

public class CopyVido {
    public static void main(String[] args) throws IOException {
        long  begin = System.currentTimeMillis();
        //byteArrayCopy();//3306
        buffStreamCopy();//1167
        long end =  System.currentTimeMillis();
        System.out.println(end - begin);
    }

    // 每次读取一个字节数组

    public static void  byteArrayCopy() throws IOException {

        FileInputStream  fis = new FileInputStream("d:\\IO\\os\\Java高级- 23.avi");
        FileOutputStream fos = new FileOutputStream("java23.avi");
        byte[] buff = new byte[1024 * 1024] ;
        int len ;
        while((len = fis.read(buff)) !=  -1){
            fos.write(buff,0,len);
        }
        fos.close();
        fis.close();
    }

    // 使用字节缓冲流来实现
    public  static void  buffStreamCopy() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\\IO\\os\\Java高级- 23.avi"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("java2323.avi"));
        byte[] buff = new byte[1024 * 1024] ;
        int len ;
        while((len = bis.read(buff)) !=  -1){
            bos.write(buff,0,len);
        }
        bis.close();
        bos.close();
    }


实现图片复制

public static void main(String[] args) throws IOException {
    // 创建一个输入缓冲流
    InputStream in = new FileInputStream("d:\\IO\\os\\mn.jpg");
    BufferedInputStream bis = new BufferedInputStream(in);
    //创建一个输出缓冲流  此处采用匿名对象创建一个OutputStream对象作为缓冲流的参数
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("buff.jpg"));// 默认的缓冲区大小8192
   byte[] buff = new byte[1024];
   int len;
    long begin= System.currentTimeMillis();
   while((len = bis.read(buff)) != -1){
       bos.write(buff,0,len);
   }
    long end = System.currentTimeMillis();
    System.out.println("复制文件完成,共耗时:" + (end - begin) + "毫秒");//1 
   bos.close();
   bis.close();


}


版权声明:本文为weixin_44283061原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。