【java】学习路径41-使用缓冲输入输出复制文件

Posted on 2022-05-04 16:25  罗芭Remoo  阅读(18)  评论(0编辑  收藏  举报

结论:Buffered+数组 这种方式速度是最快的。

    public void testBufferedIO(String source,String target){
        BufferedInputStream bufferedInputStream  = null;
        BufferedOutputStream bufferedOutputStream= null;
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream(source));
            bufferedOutputStream= new BufferedOutputStream(new FileOutputStream(target));
            byte[] data = new byte[1024];
            int length = -1;
            while ((length=bufferedInputStream.read(data))>-1){
                bufferedOutputStream.write(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }