Java代码文件复制方法比较

本文探讨了两种Java代码文件复制方法,分别使用节点流和缓冲流进行文件复制。通过实验,结论指出利用缓冲流BufferedInputStream和BufferedOutputStream进行文件复制的效率显著提高。
import java.io.*;

public class CopyFile{

        //文件通过节点流复制功能封装为方法

public void copyFile(String srcPath,String destPath){
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        //1.创建File类对象
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        //2.创建输入流和输出流对象
        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);

        //3.数据的读入和写出操作
        byte[] bytes = new byte[1024];
        int len;
        while((len = fis.read(bytes)) != -1){
            fos.write(bytes,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //4.关闭流资源
        try {
            if(fis != null){
                fis.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if(fos != null){
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

        // //文件通过缓冲流复制功能封装为方法

public void copyFileWithBuffered(String srcPath,String destPath){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            //1.创建File类对象
            File srcFile = new File(srcPath);
            File destFile = new File(destPath);

            //2.2.创建输入流和输出流对象
            //2.1创建节点流对象
            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);

            //2.2创建缓冲流对象
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            //3.复制的细节:读取、写入
            byte[] buffer = new byte[1024];
            int len;
            while((len = bis.read(buffer)) != -1){
                bos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭资源
            //要求:先关闭外层的流,再关闭内层的流
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //说明:关闭外层流的同时,内层流也会关闭
//        fos.close();
//        fis.close();
        }
    }

}

class TestCopyFile{

        public static void main(String[] args){

                CopyFile copyFile = new CopyFile();

        long start = System.currentTimeMillis();

        String srcPath = "J:\\01.rar";
        String destPath = "C:\\Users\\qqq\\Desktop\\01.rar";
        copyFile.copyFile(srcPath,destPath);

        long end = System.currentTimeMillis();

        System.out.println("复制文件成功,耗时为:" + (end - start) + "毫秒");

                System.out.println("---------------此处为分割线---------------");

                

        long start1 = System.currentTimeMillis();

        String srcPath = "J:\\01.rar";
        String destPath = "C:\\Users\\qqq\\Desktop\\01.rar";
        copyFile.copyFileWithBuffered(srcPath,destPath);

        long end1 = System.currentTimeMillis();

        System.out.println("复制文件成功,耗时为:" + (end1 - start1) + "毫秒");

        }

}

结论:使用缓冲流BufferedInputStream、BufferedOutputStream后效率明显提升。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值