文件的复制Java

1. 文本文件的复制

/**
* 实现文本文件的复制
*/
@Test
public void test4() {
    FileReader fr = null;
    FileWriter fw = null;
    try {
        //1. 实现读入和写出的File类
        File srcFile = new File("src\\echo06\\hello.txt");
        File destFile = new File("src\\echo06\\hello1.txt");


        // 2。 提供FileReader和FileWriter的类
        fr = new FileReader(srcFile);
        fw = new FileWriter(destFile);


        // 3. 读入和写出操作
        char[] cuff = new char[5];
        int len;
        while ((len = fr.read(cuff)) != -1) {
            fw.write(cuff, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // 4. 关闭流资源
        try {
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2. 非文本文件的复制

/**
* FileInputStream和FileOutputStream的使用
* 1. 对于文本文件(.txt, .java, .cpp),使用字符流处理
* 2. 对于非文本文件(.jpg, .mp3, .avi, .doc, .ppt),使用字节流处理
*
* 实现复制图片
*/
@Test
public void test5() {


    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        File srcFile = new File("src\\echo06\\earth.jpg");
        File destFile = new File("src\\echo06\\earth1.jpg");


        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);


        byte[] buffer = new byte[5];
        int len;
        while ((len = fis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        System.out.println("success");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3. 完整文件复制

public class CopyFile {
    public static void main(String[] args) {
        CopyFile cf = new CopyFile();
        long start = System.currentTimeMillis();


        String srcPath = "D:\\Data\\earth.mp4";
        String destPath = "D:\\Data\\earth(1).mp4";


        // 字节流的方式
//        cf.copyFileStream(srcPath, destPath);

        // 缓冲流的方式
        cf.copyFileBuffered(srcPath, destPath);

        long end = System.currentTimeMillis();

        System.out.println("spend time:" + (end - start));

    }


    public void copyFileStream(String srcPath, String destPath) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File srcFile = new File(srcPath);
            File destFile = new File(destPath);
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);


            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            System.out.println("success");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert fos != null;
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 缓冲流:提高读写速度
     * BufferedInputStream和BufferedOutputStream实现复制操作
     * 提高读写速度的原因:内部提供一个缓冲区
     * 处理流:套接在已有流的基础上
     *
     */
    public void copyFileBuffered(String srcPath, String destPath) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            File srcFile = new File(srcPath);
            File destFile = new File(destPath);


            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);


            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);


            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. 资源关闭
            // 要求:先关闭外层的流,在关闭内层的流
            try {
                assert bos != null;
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 关闭外层流的同时,内层流也会自动进行关闭
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值