博客
关于我
java_递归复制目录及目录下的文件
阅读量:237 次
发布时间:2019-03-01

本文共 4429 字,大约阅读时间需要 14 分钟。

package experiment8.exp1;import java.io.File;import java.io.IOException;import java.nio.file.Files;import java.util.Scanner;public class CopyDirectoryAndFiles{       public static void main(String[] args) throws IOException {           Scanner scanner = new Scanner(System.in);        System.out.println("输入被复制源目录名:");        String sourceDirectory = scanner.nextLine();        System.out.println("输入副本目标目录名");        String destinationDirectory = scanner.nextLine();        /*调用递归复制函数.*/        copyDir(sourceDirectory, destinationDirectory);    }/*本递归复制函数具有检查并创建新目录的能力,*/    static void copyDir(String oldPath, String newPath) throws IOException {           File file = new File(oldPath);        String[] filePath = file.list();/*//当前路径下存在的目录名和文件名的数组.比较建议用list方法,不推荐用listFile方法,因为后面        要通过连接目录名来创建新目录,用字符串比较方便.*/        /*检查新目录是否存在.若不存在,则创建一个*/        if (!(new File(newPath)).exists()) {               (new File(newPath)).mkdir();//创建目录        }        /*遍历当前目录下的子目录和文件*///        for (int i = 0; i < filePath.length; i++)        for (String x : filePath) {               /*如果这(x)是一个子目录:(执行递归)*/            if ((new File(oldPath + File.separator + x)).isDirectory()) {                   copyDir(oldPath + File.separator + x, newPath + File.separator + x);            }/*java.io.Filepublic static final String separator //这个separator属性java.io软件包下的File类的一个公开的静态且是常量的属性(成员变量),是方便的智能的分割符.The system-dependent default name-separator character, represented as a string for convenience. This string contains a single character, namely separatorChar.*/            /*如果x所指的是个文件:执行复制即可*/            if (new File(oldPath + File.separator + x).isFile()) {                   File source = new File(oldPath + File.separator + x);                File dest = new File(newPath + File.separator + x);                if (!(dest.exists())) {                       Files.copy(source.toPath(), dest.toPath());//应当创建新文件的能力                }            }        }//endFor        System.out.println("The replication operation has been successfully executed!");    }//endCopyFunction}

说明并修正某些内容,对比

package experiment8.exp1;import java.io.File;import java.io.IOException;import java.nio.file.Files;import java.util.Scanner;public class CopyDirectoryAndFiles {       public static void main(String[] args) throws IOException {           Scanner scanner = new Scanner(System.in);        System.out.println("输入被复制源目录名:");        String sourceDirectory = scanner.nextLine();        System.out.println("输入副本目标目录名");        String destinationDirectory = scanner.nextLine();        /*调用递归复制函数.*/        copyDir(sourceDirectory, destinationDirectory);    }    /*本递归复制函数具有检查并创建新目录的能力,*/    static void copyDir(String oldPath, String newPath) throws IOException {           File file = new File(oldPath);        String[] filePath = file.list();/*//当前路径下存在的目录名和文件名的数组.比较建议用list方法,不推荐用listFile方法,因为后面        要通过连接目录名来创建新目录,用字符串比较方便.*/        /*检查新目录是否存在.若不存在,则创建一个(搭配规则1使用的,对参数的尾缀有一致性要求        否则有些细微差异,就是第一层的目录结构要丢失)*/ /*       if (!(new File(newPath)).exists()) {            (new File(newPath)).mkdir();//创建目录        }*/        /*规则2使用;对路径名的尾缀没有要求的*/        if (file.isDirectory()) {               //System.out.println(file.getName());            (new File(newPath + File.separator + file.getName())).mkdir();            newPath = newPath + File.separator + file.getName();        }        /*遍历当前目录下的子目录和文件*///        for (int i = 0; i < filePath.length; i++)        for (String x : filePath) {               /*如果这(x)是一个子目录:(执行递归)*/            if ((new File(oldPath + File.separator + x)).isDirectory()) {                   //此处传入的新旧路径名的后缀都是一样的(x)结尾。                /*所以为了保证统一性,在最初的使用(调用此递归函数时,对参数的形式上就有所约定。                 * 你有两中方向可选。                 * 1、在最初的调用中,就把路径名处理成尾缀一致的。                 * 2.在函数的递归的编写就不要提前处理成尾缀一致的。*/                //这里我们这里尝试一下2。                copyDir(oldPath + File.separator + x, newPath + File.separator);            }            /*如果x所指的是个文件:执行复制即可*/            if (new File(oldPath + File.separator + x).isFile()) {                   File source = new File(oldPath + File.separator + x);                File dest = new File(newPath + File.separator + x);                if (!(dest.exists())) {                       Files.copy(source.toPath(), dest.toPath());//应当创建新文件的能力                }            }        }//endFor        System.out.println("The replication operation has been successfully executed!");    }//endCopyFunction}

转载地址:http://avzt.baihongyu.com/

你可能感兴趣的文章
nfs mount 故障 mount.nfs: access denied by server while mounting 10.0.100.208:/backup_usb
查看>>
NFS Server及Client配置与挂载详解
查看>>
NFS 服务配置篇
查看>>
NFS共享文件系统搭建
查看>>
nfs复习
查看>>
NFS安装配置
查看>>
NFS服务器配置-服务启动与停止
查看>>
NFS的安装以及windows/linux挂载linux网络文件系统NFS
查看>>
NFS的常用挂载参数
查看>>
NFS网络文件系统
查看>>
NFS远程目录挂载
查看>>
nft文件传输_利用remoting实现文件传输-.NET教程,远程及网络应用
查看>>
NFV商用可行新华三vBRAS方案实践验证
查看>>
ng build --aot --prod生成文件报错
查看>>
ng 指令的自定义、使用
查看>>
ng6.1 新特性:滚回到之前的位置
查看>>
nghttp3使用指南
查看>>
【Flink】Flink 2023 Flink 自动化运维的大规模落地实践
查看>>
Nginx
查看>>
nginx + etcd 动态负载均衡实践(一)—— 组件介绍
查看>>