19.7. 使用字节流复制文件

FileCopy.java

package com.a51work.com;

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

public class FileCopy {
    public static void main(String[] args) {
        try (FileInputStream in = new FileInputStream("D:\\GitHub\\Python-master.zip");
             FileOutputStream out = new FileOutputStream("D:\\GitHub\\Python-master-副本.zip")) {

//            开始时间,当前系统的纳秒时间
            long startTime = System.nanoTime();

            //准备一个缓冲区
            byte[] buffer = new byte[1024];
            // 读取一次
            int len = in.read(buffer);

            while (len != -1) {
                String copyStr = new String(buffer);
                //打印复印的字符串
                System.out.println(copyStr);

                // 开始写入数据
                out.write(buffer, 0, len);
                // 再读取一次
                len = in.read(buffer);

            }

//            结束时间,当前系统的纳秒时间
            long End_Time = System.nanoTime() - startTime;
            System.out.println("耗时:" + (End_Time / 1000000.0) + "毫秒");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

/*
耗时:582.925483毫秒
* */