2017-06-14 33 views
0

我想以後重新命名他們,但不斷收到錯誤,從1個目錄中的文件複製到另一個:試圖複製文件並獲得「文件不存在」的錯誤

Exception in thread "main" java.nio.file.NoSuchFileException: C:\Users\talain\Desktop\marketingOriginal\FX Rates\FY17\Q11\Week_12___February_12_2016.xls -> C:\Users\talain\Desktop\fakeOnedrive\FX Rates\FY17\Q11\0 

我的代碼:

public class shortenFilenameClass 
{ 
    static String absolutePathLocal = "C:\\Users\\talain\\Desktop\\marketingOriginal".replace('\\', '/'); //path to original files 
    static String absolutePathOnedrive= "C:\\Users\\talain\\Desktop\\fakeOnedrive".replace('\\', '/'); //path to onedrive 

     public static void main(String[] args) throws IOException 
     {   
      System.out.println(absolutePathLocal.length()); 
      File local = new File(absolutePathLocal); 
      File onedrive = new File(absolutePathOnedrive); 
      int fileCount = 0; 

      File[] filesInDir = local.listFiles(); 
      manipulateFiles(filesInDir, fileCount); 
     } 

     public static void manipulateFiles(File[] filesInDirPassed, int fileCount) throws IOException 
     { 
      for(int i = 0; i < filesInDirPassed.length; i++) 
      { 
        File currentFile = filesInDirPassed[i]; 
        if(currentFile.isDirectory()) 
        { 
         String local = currentFile.getAbsolutePath(); 
         //String onedrive = current 
         manipulateFiles(currentFile.listFiles(), fileCount); 
        } 
        String name = currentFile.getName(); 
        System.out.println("old filename: " + name); 
        String newName = String.valueOf(fileCount); 
        fileCount++; 


        File oldPath = new File(currentFile.getAbsolutePath()); 
        System.out.println("oldPath: " + oldPath); 

        //currentFile.renameTo(new File(oldPath.toString())); 
        System.out.println("currentFile: " + currentFile); 

        String pathExtension = new String(currentFile.getAbsolutePath().substring(42)); 
        pathExtension = pathExtension.replaceAll(name, newName); 
        File newPath = new File(absolutePathOnedrive + "/" + pathExtension); 
        System.out.println("newPath: " + newPath); 
        copyFileUsingJava7Files(oldPath, newPath); 

        File finalPath = new File(absolutePathOnedrive + "" + name); 
        //newPath.renameTo(new File(finalPath.toString())); 
        //copyFileUsingJava7Files(newPath, finalPath); 
        System.out.println("renamed: " + name + "to: " + newName + ", copied to one drive, and changed back to original name"); 
      } 
     } 


     private static void copyFileUsingJava7Files(File source, File dest) throws IOException { 
      Files.copy(source.toPath(), dest.toPath()); 
     } 
} 
+0

不,你不是。你正在得到'NoSuchFileException'。所有的目標目錄都存在嗎? NB'File oldPath = new File(currentFile.getAbsolutePath());'完全等同於'File oldPath = currentFile;'。 – EJP

回答

0

Java被告知C:\Users\talain\Desktop\marketingOriginal\FX Rates\FY17\Q11\Week_12___February_12_2016.xls -> C:\Users\talain\Desktop\fakeOnedrive\FX Rates\FY17\Q11\0是一個文件,這是不正確的。檢查錯誤源自哪裏,看看你是否在任何地方使用lambda表達式。

+0

我可以。整個消息更可能來自'Files.copy()',同時顯示源和目標,以幫助調試。 – EJP

+0

這不是這個例外的作用。 'NoSuchFileException'顯示源文件。 –

相關問題