有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将多个文件夹移动到一个文件夹中

我有一个目录C:\Test\,其中有几个文件和文件夹

 example :
 C:\Test\new1.txt
 C:\Test\document.xls
 C:\Test\presentation.pdf
 C:\Test\pro_country
 C:\Test\pro_libs
 C:\Test\misc
 C:\Test\pro_bin
 C:\Test\mug

我想将所有以pro开头的文件夹移动(而不是复制)到一个文件夹中

所以我会有这样的灾难

 example :
 C:\Test\new1.txt
 C:\Test\document.xls
 C:\Test\presentation.pdf
 C:\Test\misc
 C:\Test\mug
 C:\Test\Newprofolder

我尝试了这段代码,但我不知道如何从列表文件返回结果,以便将它们复制到目录中

File[] proList = direct.listFiles(new FilenameFilter() {
    @Override
    public boolean accept(File dir, String name) {

        return name.startsWith("Pro");
    }
});

String pFileDest = directory + "//" + "pcore.war";
File filepldst = new File(pFileDest);
File filePortalSrc = new File(pLocation);

try {
    FileUtils.copyFile(filePortalSrc, filepldst);
} catch (IOException e) {
    e.printStackTrace();
}

共 (1) 个答案

  1. # 1 楼答案

    Files.move(new File("C:\\projects\\test").toPath(), new File("C:\\projects\\dirTest").toPath(), StandardCopyOption.REPLACE_EXISTING);
    

    更改源和目标路径