首页 > 其他 > 详细

缓冲流复制文件与基本流复制文件比较

时间:2020-10-11 18:55:26      阅读:51      评论:0      收藏:0      [点我收藏+]
import javafx.scene.control.ButtonBar;

import java.io.*;

public class Demo03Copy2 {
    public static void main(String[] args) throws IOException {
        long s=System.currentTimeMillis();

        BufferedInputStream bis=new BufferedInputStream(new FileInputStream("F:\\basic\\untitled13\\src\\it\\cast\\day15\\demo02\\1.jpeg"));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("F:\\basic\\untitled13\\src\\it\\cast\\day15\\demo02\\3.jpeg"));
        /*
        int len=0;
        while ((len=bis.read())!=-1){
            bos.write(len);
        }//复制文件共耗时:34毫秒*/

        byte[] bytes1=new byte[1024];
        int len=0;
        while ((len=bis.read(bytes1))!=-1){
            //System.out.println(new String(bytes1,0,len));//读取有效的几个
            bos.write(bytes1,0,len);
        }//复制文件共耗时:5毫秒

        bis.close();
        bos.close();
        long e=System.currentTimeMillis();
        System.out.println("复制文件共耗时:"+(e-s)+"毫秒");
    }
}

 

 

基本流复制文件:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

//文件的复制
public class Demo03Copy {
    public static void main(String[] args) throws IOException {
        long s=System.currentTimeMillis();
        FileInputStream fis=new FileInputStream("F:\\basic\\untitled13\\src\\it\\cast\\day15\\demo02\\1.jpeg");
        FileOutputStream fos=new FileOutputStream("F:\\basic\\untitled13\\src\\it\\cast\\day15\\demo02\\2.jpeg");
/*        byte[] bytes1=new byte[1024];
        int len=0;
        while ((len=fis.read(bytes1))!=-1){
            //System.out.println(new String(bytes1,0,len));//读取有效的几个
            fos.write(bytes1,0,len);
        }//复制文件共耗时:10毫秒*/

        int len=0;
        while((len=fis.read())!=-1){
            fos.write(len);

        }//复制文件共耗时:4502毫秒
        fos.close();
        fis.close();
        long e=System.currentTimeMillis();
        System.out.println("复制文件共耗时:"+(e-s)+"毫秒");

    }
}

 复制的文件大小是359 KB (367,976 字节)的jpeg格式的图片,在同一文件夹下面复制文件。

缓冲流复制文件与基本流复制文件比较

原文:https://www.cnblogs.com/cy2268540857/p/13797664.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!