将规定目录下全部内容拷贝到指定目录下

将规定目录下全部内容拷贝到指定目录下,代码附有详细注解,综合运用IO流,文件类方法,递归算法

package test;

import java.io.*;

public class IoCopy {
    public static void main(String[] args) {
        //源文件位置
        File srcFile = new File("D:\\IDEAproject\\javase\\new\\out\\production\\new");
        //目标文件位置
        File destFile = new File("C:\\aa");
        copyMethod(srcFile, destFile);
    }
    private static void copyMethod(File srcFile, File destFile) {
        //如果源文件是文件类型,则...
        if(srcFile.isFile()){
            //设置路径为目标文件路径加上(源文件路径剪去前四个字符),即剪去"d:\\"
            String path = (destFile.getAbsolutePath().endsWith("\\")? destFile.getAbsolutePath() :
                    destFile.getAbsolutePath() + "\\") + srcFile.getAbsolutePath().substring(3);
                    /*//File eachFile = new File(path);
                    //eachFile.getParent();*/
            //尝试输入流读取,将流的创建放在try参数里,待到catch结束可自动关闭流,JDK7之后的新特性
            try(FileInputStream fis = new FileInputStream(srcFile)) {
                //FileOutputStream可自动创建文件
                try(FileOutputStream fos = new FileOutputStream(path)){
                    //创建数组
                    byte[] bytes = new byte[1024 * 1024];
                    int readCount  ;
                    //将读出来的字节赋给readCount,读完之后返回-1,跳出循环
                    while ((readCount = fis.read(bytes)) != -1) {
                        fos.write(bytes,0,readCount);
                    }
                    //刷新输出流
                    fos.flush();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        //将源文件目录的所有文件放在一个文件数组里
        File[] files = srcFile.listFiles();
        //遍历源文件目录下的文件
        for(File f :files){
            //如果文件f还是一个目录,则...
            if(f.isDirectory()){
                //获取f的绝对路径
                String srcDir = f.getAbsolutePath();
                //获得目标目录为main方法里的目标目录加上(源文件绝对路径剪去“D:\\”)
                String destDir = (destFile.getAbsolutePath().endsWith("\\")? destFile.getAbsolutePath() :
                        destFile.getAbsolutePath() + "\\") + srcDir.substring(3);

                File newFile = new File(destDir);
                //创建目录
                if(!newFile.exists()){
                    newFile.mkdirs();
                }
                System.out.println(destDir);
            }
            /**以f作为实参调用自己,递归还是画图比较好理解,可以自己画一下压栈和弹栈图,
             * 每调用一个方法就压栈一次,每次调用完该方法就弹栈出来,程序返回到前一次压栈点,
             * 所以栈内存遵循先进后出和后进先出原则,就像是子弹夹...
             * 一个标准的程序运行是以压栈main方法作为开始标志,以弹栈main方法作为结束标志。
            */
            copyMethod(f,destFile);
        }

    }


}

模仿老师写的代码,自己琢磨了许久感觉理解透彻了,然后改善了一下代码,运行正确才写的注释。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值