【发布时间】:2021-12-01 08:47:52
【问题描述】:
问了这个问题,已经在堆栈上的其他问题中尝试过可能的解决方案,但这并没有让我解决问题。
如标题所示,我创建了一个 java 实用程序,我必须使用它对文本文件执行操作,特别是我必须执行简单的操作以在目录之间移动、从一个目录复制到另一个目录等。
为此,我使用了 java 库 java.io.File 和 java.nio.*,并且我现在已经实现了两个函数,copyFile(sourcePath, targetPath) 和 moveFile(sourcePath, targetPath)。
为了开发这个我使用的是mac,文件在源路径/Users/myname/Documents/myfolder/F24/archive/下,我的目标路径是/Users/myname/Documents/myfolder/F24/target/。
但是当我运行我的代码时,我得到一个java.nio.file.NoSuchFileException: /Users/myname/Documents/myfolder/F24/archive
已经尝试过堆栈和 java 文档中的其他解决方案,但我还不能解决这个问题......我接受任何建议或建议
谢谢大家
- 我的代码:
// copyFile: funzione chiamata per copiare file
public static boolean copyFile(String sourcePath, String targetPath){
boolean fileCopied = true;
try{
Files.copy(Paths.get(sourcePath), Paths.get(targetPath), StandardCopyOption.REPLACE_EXISTING);
}catch(Exception e){
String sp = Paths.get(sourcePath)+"/";
fileCopied = false;
System.out.println("Non posso copiare i file dalla cartella "+sp+" nella cartella "+Paths.get(targetPath)+" ! \n");
e.printStackTrace();
}
return fileCopied;
}
【问题讨论】:
-
先试试
chmod -R 777 /Users/myname/Documents/myfolder/F24/archive/? -
您是否要移动或复制目录,包括其内容?如果是,请检查this question
-
有时监督:targetPath 不能是目录,而是(可能还不存在的)目标文件的完整路径 - 带有文件名。复制选项也很投标。
-
嗨,作为第一个操作,我给了文件夹 777 权限,但它一直给我例外......让我感到困惑的是,我拥有文件的路径是
/Users/myname/Documents/myfolder/F24/archive/but the异常将我显示为路径`/Users/myname/Documents/myfolder/F24/archive`,即最后一个/丢失了......我不知道这是否可能 -
@deHaar 不,我只想复制文件而不是目录
标签: java exception java.nio.file