我遇到了一些與我的程序有關的問題,它給了我java.nio.file.NoSuchFileException
我試圖將文件夾和文件複製到另一個,但其他人也在工作。我試圖從Arraylist
複製文件夾和文件從arraylist的值是從屬性文件DEL_COPY_DIR
。這是我的代碼如下。在java中沒有這樣的文件例外
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:我正在使用文本框來源和目標
是否存在文件'D:\ dest \ data \ 25 \ misc \ AlarmCum.obj'?目錄是否存在?D:\ destination \ data \ 25 \ misc? – Andreas
@Andreas是存在的文件,但在目錄中它不是,但其他文件可以順利傳輸,但是當文件給出錯誤時。 – user123040569
所以,其他文件可以複製到其他存在的目錄,但是這個文件不能被複制到一個不存在的目錄中。嗯......對不起。什麼讓你對錯誤信息感到困惑?目標目錄不存在,因此錯誤。 – Andreas