用文件字节流和File类实现目录的拷贝

用文件字节流和File类实现目录的拷贝:

将:C:\Users\win10\Desktop\Photos
拷贝到:F:\

提示:
用到的类FileInputStream,FileOutputStream,File。
用到的思想:递归。

测试代码:

import java.io.*;

public class Test02 {
    public static void main(String[] args) {
        File srcFile = new File("C:\\Users\\win10\\Desktop\\Photos");
        File destFile = new File("F:\\");

        Test02.myCopy(srcFile, destFile);
    }

    // 递归读目录
    public static void myCopy(File srcFile, File destFile) {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            if (srcFile.isFile()) {
                //System.out.println(srcFile.getName());
                // srcFile是个文件,那就在目标处创建一个同名文件
                String destPath = destFile.getAbsolutePath();
                destPath = destPath.endsWith("\\") ? destPath : destPath + "\\";
                
                File dest = new File(destPath + srcFile.getName() );
                if (!dest.exists()) {
                    dest.createNewFile();
                }
                System.out.println(dest);

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

                // 开始读写
                byte[] bytes = new byte[1024 * 1024];  // 缓冲区为1MB
                int readCount = 0;
                while ((readCount = fis.read(bytes)) != -1) {
                    fos.write(bytes, 0, readCount);
                }
                
                // 输出完毕后刷新
                fos.flush();
                return;  // 递归结束
                // 注意:try..catch中会先执行finally再执行try中的return语句
                
            }
            // 程序执行到这里,说明srcFile是个目录,在目标处创建一个同名目录
            //System.out.println(srcFile.getName());
            String destPath = destFile.getAbsolutePath();
            destPath = destPath.endsWith("\\") ? destPath : destPath + "\\";
            
            File destDir = new File(destPath + srcFile.getName());
            if (!destDir.exists()) {
                destDir.mkdir();
            }
            System.out.println(destDir);

            // 拷贝完目录后,拷贝目录的子文件
            File[] files = srcFile.listFiles();
            for (File f : files) {
                myCopy(f, destDir);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭输入输出流
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值