【发布时间】:2021-12-07 10:37:51
【问题描述】:
我必须编写一个 java 方法 public void public void copyTo(Path rSource, Path rDest) 将所有文件从现有目录 rSource 复制到具有相同名称的新目录 rDest。 rSource 必须存在且 rDest 必须不存在,如果不存在则运行时异常。我似乎无法让它工作,帮助!
我尝试了什么:
public void copyTo(Path rSource, Path rDest){
if(!(Files.exists(rSource) && Files.isDirectory(rSource)) || (Files.exists(rDest))){
throw new RuntimeException();
}
try {
Files.createDirectory(rDest);
if(Files.exists(rDest)){
try(DirectoryStream<Path> stream = Files.newDirectoryStream(rSource)) {
for(Path p : stream) {
System.out.println(p.toString());
Files.copy(p, rDest);
}
} catch( IOException ex) {
}
}
} catch (IOException e) {
}
}
【问题讨论】:
-
欢迎来到 Stack Overflow。我强烈建议您使用 tour 了解 Stack Overflow 的工作原理并阅读 How to Ask。这将帮助您提高问题的质量。 对于每个问题,请显示您尝试过的尝试以及您从尝试中得到的错误消息。