2015-05-21 35 views
1

我想用這個java代碼移動文件,它可以找到文件但不移動它,只是刪除我將它移動到的目錄。用java移動文件

public void ch() throws Exception{ 
    if (FC.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){ 
     java.io.File file = FC.getSelectedFile(); 
     Scanner input = new Scanner(file); 
     System.out.println(file); 

     Path source = Paths.get(file + ""); 
     Path target = Paths.get("C:\\Users\\Marcus\\Desktop\\2");  
     try { 
      Files.move(source, target, REPLACE_EXISTING); 
     } catch (IOException e){ 
      System.out.println("Failed to move the file"); 
     } 
    }else{ 
     System.out.println("?"); 
    } 
} 

回答

0

如果你想使用Files.move(),你的目標路徑可能應該是目標文件的完整路徑,而不是你想放置目標文件的目標目錄。

Path target = Paths.get("C:\\Users\\Marcus\\Desktop\\2\\" + source.getName()); 
0

您應該使用Files.copy()代替Files.move()

1

在您的目的地路徑的末尾添加文件名,如下圖所示: 您可以用File.ranameTo()方法移動文件,就像這樣:

file.renameTo(new File("C:\\Users\\Marcus\\Desktop\\2\\"+file.getName())); 

在您的例子:

public void ch() throws Exception{ 
     if (FC.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){ 
      java.io.File file = FC.getSelectedFile();  
      try { 
       file.renameTo(new File("C:\\Users\\Marcus\\Desktop\\2\\"+file.getName())); 
      } catch (Exception e){ 
       System.out.println("Failed to move the file"); 
      } 
     }else{ 
      System.out.println("?"); 
     } 
    } 
0

我強烈建議使用第三方工具,如Apache Commons IO的FileUtil類來執行此類操作。

例如:FileUtil.moveFileToDirectory

使用這些類型的實用工具有許多問題可以節省你,你甚至不知道潛伏。是的,這些常見的應用程序有一些限制,但在簡單情況下,好處通常會超過它們。

谷歌番石榴也是一種選擇,但我在那裏的經驗較少。

+1

如果一個開發人員可以使用Java核心API,你爲什麼會推薦使用第三方庫? 'java.nio.file.Files'將執行該操作而不需要第三方jar。 – MadConan

+0

有可能我只是老派,喜歡我喜歡的工具:-D但是,我建議語義清晰度支持Apache utils。你知道100%發生了什麼,並且你的參數通過moveToDirectory()調用來驗證。這似乎是OP首先被絆倒的部分。 –

0

您的代碼很接近但存在一些潛在的問題。在我開始之前,我應該說我使用的是Mac(因此改變了路徑),所以雖然這對我有用,但我的系統中可能存在一些我無法解釋的基礎權限問題。

1)您沒有使用要移動到的文件的名稱。您正在使用您想要將文件移動到的目錄。這是一個合理的假設,但您需要將其設置爲完全合格的路徑文件名。

2)您正在創建Scanner而不是使用它。這可能並不重要,但最好是消除不必要的代碼。

3)您不驗證獲取從Files.move()返回的Path實例創建的路徑。

這是我的示例代碼。我測試了它,它工作正常。再次,我正在使用Mac,因此請考慮這一點。

public void moveFile(){ 
    JFileChooser fc = new JFileChooser("/"); 

    if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){ 
     File file = fc.getSelectedFile(); 
     System.out.println("Chosen File: " + file.getAbsolutePath()); 

     String newFileName = System.getProperty("user.home")+File.separator+file.getName(); 
     System.out.println("Attempting to move chosen file to destination: " + newFileName); 

     Path target = Paths.get(newFileName); 

     try { 
      Path newPath = Files.move(file.toPath(), target, REPLACE_EXISTING); 
      System.out.println("Path returned from move: " + newPath); 

     } catch (IOException e){ 
      // Checked exceptions are evil. 
      throw new IllegalStateException("Unable to move the file: " + file.getAbsolutePath(),e); 
     } 
    } 
} 

從測試的一個輸出:

Chosen File: /Users/dombroco/temp/simpleDbToFileTest1.txt 
Attempting to move chosen file to destination: /Users/dombroco/simpleDbToFileTest1.txt 
Path returned from move: /Users/dombroco/simpleDbToFileTest1.txt