BufferedOutputStream 和 BufferedInputStream复制文件实例

渡星河
2023-01-06 / 0 评论 / 30 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年01月06日,已超过622天没有更新,若内容或图片失效,请留言反馈。

我们复制一下元神的安装包

package Files;

import java.io.*;

public class TestFileOutputStream {
    public static void main(String[] args) {
        //fileOutput();
        //fileOutput();
       // inputOutput();
        //拷贝元神
        bufferedOutput();
    }
    public static void bufferedOutput() {
      //记录拷贝需要的时间
            long start = System.currentTimeMillis();
            System.out.println("开始拷贝元神");
            try (BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream("E:\\原神.exe"))) {
                try (BufferedInputStream input = new BufferedInputStream(new FileInputStream("E:\\test\\元神.exe"))){
                    int b;
                    while ((b = input.read()) != -1) {
                        bufferedOutput.write(b);
                    }
                }catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            System.out.println("时间:"+(System.currentTimeMillis() - start));
        }
}

1

Reader 和 Writer不能复制非纯文本文件

会导致损坏

package Files;

import java.io.*;

public class TestWriter {
    public static void main(String[] args) {
        System.out.println("开始");
        long start = System.currentTimeMillis();
        try(Reader reader = new FileReader(new File("E:\\test\\元神.exe"))) {
            int b ;
            try(Writer writer = new FileWriter(new File("E:\\原神1.exe"))){
                while ((b = reader.read()) != -1) {
                    writer.write(b);
                }
            }
             catch (IOException e) {
                throw new RuntimeException(e);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("结束00");
        System.out.println(System.currentTimeMillis() - start);

    }
}
0

评论 (0)

取消