我的程序遇到了一些问题,它给了我java.nio.file.NoSuchFileException
我正在尝试将文件夹和文件复制到另一个,但其他人也在工作。我正在尝试从Arraylist
复制文件夹和文件来自arraylist的值来自属性文件DEL_COPY_DIR
。这是我的代码。
ArrayList<String> list1 = readConfigFileList(ConstantVariables.DEL_COPY_DIR);
for (String strList1 : list1)
{
if(strList1.contains("<mnbr>")){
String[] saDirectory = strList1.split("<mnbr>");
String strDirectory = saDirectory[0];
String strMnbrContent = saDirectory[1];
File file = new File(strSource + strDirectory);
String[] saMnbrFile = file.list();
for(int i = 0; i < saMnbrFile.length; i++) {
File fileList = new File(strSource + strDirectory + saMnbrFile[i] + strMnbrContent);
String strsrcList = new String(fileList.toString());
File fileList1 = new File(strDestination + strDirectory + saMnbrFile[i] + strMnbrContent);
String strdestList = new String(fileList1.toString());
if(fileList.isDirectory())
// System.out.println(strSource + strDirectory + saMnbrFile[i] + strMnbrContent);
copyFolders(strsrcList, strdestList);
}
}
else {
copyFolders(strSource + strList1 , strDestination + strList1);
}
}
复制文件
public void copyFiles(String source, String destination) throws IOException{
try {
File fileFrom = new File(source);
File fileTo = new File(destination);
Files.copy( fileFrom.toPath(), fileTo.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error");
}
}
复制文件夹
public void copyFolders(String src, String dest)
throws IOException{
File srcFrom = new File(src);
File destTo = new File(dest);
if(srcFrom.isDirectory()){
if (!destTo.exists())
{
destTo.mkdir();
txtDetails.append("Directory copied : " + dest + "\n");
}
final String files[] = srcFrom.list();
for (String file : files)
{
File srcFile = new File(src, file);
File destFile = new File(dest, file);
//Recursive function call
copyFolders(srcFile.toString(), destFile.toString());
}
}
else{
copyFiles(src, dest);
txtDetails.append("Files Copying: " + srcFrom.getAbsolutePath() + "...Done" + "\n");
}
}
完整错误
java.nio.file.NoSuchFileException: D:\dest\data\25\misc\AlarmCum.obj -> D:\destination\data\25\misc\AlarmCum.obj
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileCopy.copy(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.copy(Unknown Source)
at java.nio.file.Files.copy(Unknown Source)
at org.eclipse.wb.swt.FortryApplication.copyFiles(FortryApplication.java:295)
at org.eclipse.wb.swt.FortryApplication.copyFolders(FortryApplication.java:337)
at org.eclipse.wb.swt.FortryApplication.copyFolders(FortryApplication.java:332)
at org.eclipse.wb.swt.FortryApplication$3.widgetSelected(FortryApplication.java:239)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at org.eclipse.wb.swt.FortryApplication.open(FortryApplication.java:51)
at org.eclipse.wb.swt.FortryApplication.main(FortryApplication.java:346)
PS:我正在使用文本框作为源和目标
答案 0 :(得分:-3)
此异常表示尝试访问不存在的文件。
在您的情况下,它是D:\dest\data\25\misc\AlarmCum.obj
。
我建议检查拼写错误的路径并确保它在文件系统上。
如果您自动创建文件,请确保在访问之前创建该文件。 使用此检查
if(yourFile.exists()){
//your logic here
}
else {
//your logic here when file doesn't exist
}
答案 1 :(得分:-3)
正如异常所说,文件在突出显示的路径中不存在,您应该尝试调试方式。
首先尝试使用少量文件运行程序,使其工作,然后转到更通用的文件数。