| 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(); |
| |
| |
| if (!(new File(newPath)).exists()) { |
| (new File(newPath)).mkdir(); |
| } |
| |
| |
| for (String x : filePath) { |
| |
| if ((new File(oldPath + File.separator + x)).isDirectory()) { |
| 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()); |
| } |
| |
| } |
| } |
| System.out.println("The replication operation has been successfully executed!"); |
| } |
| } |
说明并修正某些内容,对比
| 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(); |
| |
| |
| |
| |
| |
| |
| |
| |
| if (file.isDirectory()) { |
| |
| (new File(newPath + File.separator + file.getName())).mkdir(); |
| newPath = newPath + File.separator + file.getName(); |
| } |
| |
| |
| for (String x : filePath) { |
| |
| if ((new File(oldPath + File.separator + x)).isDirectory()) { |
| |
| |
| |
| |
| |
| |
| copyDir(oldPath + File.separator + x, newPath + File.separator); |
| } |
| |
| |
| 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()); |
| } |
| |
| } |
| } |
| System.out.println("The replication operation has been successfully executed!"); |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2022-01-04 动态规划_(dynamic programming)_python_最大子序列(最长公共子序列)(可非连续子序列(several versions))
2022-01-04 linux_命令行描述语言docopt:man手册中的记号解释