如何移动文件夹和文件?

问题描述:

我的程序使用文件数据库,我想知道如何移动文件夹而不删除文件夹中的文件。我正在使用java。当我按下按钮时,我希望它们移动到指定位置。按钮上的代码如下所示:如何移动文件夹和文件?

private void uploadButtonActionPerformed(java.awt.event.ActionEvent evt) {            
    // TODO add your handling code here: 
    ProjectInfo.documentTitle = fileName.getText(); 
    ProjectInfo.moveFileLocation = fileSpecificLocation.getText(); 
    String name = userNameText.getText(); 
    Signup.fileToMoveTo = "C:\\CloudAurora\\" + name + "\\"; 
    String docTtl = ProjectInfo.documentTitle; 

    // docTtl.renameTo(new File(Signup.fileToMoveTo)); 

    ProjectInfo.documentTitle = fileName.getText(); 
    ProjectInfo.moveFileLocation = fileSpecificLocation.getText(); 
    String name = userNameText.getText(); 
    Signup.fileToMoveTo = "C:\\CloudAurora\\" + name + "\\"; 
    String docTtl = ProjectInfo.documentTitle; 
    System.out.println(ProjectInfo.documentTitle); 
    System.out.println(Signup.fileToMoveTo); 
} 

如果有人能帮上忙,那就太棒了。我已经找到了一种方法来做到这一点,但无法弄清楚如何

+0

查看代码[posted here](https://www.mkyong.com/java/how-to-copy-directory-in-java/),了解如何做到这一点的体面示例。 – DevilsHnd

我希望根据你的情况,我已经正确地提到了源文件和目标文件。下面的代码应该随文件一起移动文件夹。

File srcFile = new File(docTtl); 
File destFile = new File(Signup.fileToMoveTo); 

/* Handle IOException for the below line */ 
Files.move(Paths.get(srcFile.getPath()), Paths.get(destFile.getPath()), StandardCopyOption.REPLACE_EXISTING);