Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除java目录中的某些文件夹_Java_Path_Directory - Fatal编程技术网

删除java目录中的某些文件夹

删除java目录中的某些文件夹,java,path,directory,Java,Path,Directory,假设我在具有路径的目录中有一个文件夹名称列表 C:\Users\Desktop\Application\(“文件夹名称”) 如何删除此目录中的某些文件夹并将已存储的文件夹保留在列表中 我有一个字符串列表: String[]deleteList=“文件夹1、文件夹2、文件夹3” 在目录中: C:\Users\Desktop\Application\我只想删除文件夹2 如何使用字符串列表而不是显式调用该文件夹 到目前为止,我已经: 更新: File[] deleteList = directory.

假设我在具有路径的目录中有一个文件夹名称列表

C:\Users\Desktop\Application\(“文件夹名称”)

如何删除此目录中的某些文件夹并将已存储的文件夹保留在列表中

我有一个字符串列表:

String[]deleteList=“文件夹1、文件夹2、文件夹3”

在目录中:

C:\Users\Desktop\Application\
我只想删除
文件夹2

如何使用字符串列表而不是显式调用该文件夹

到目前为止,我已经:

更新:

File[] deleteList = directory.listFiles(fileFilter);
        for (File file : deleteList) {
            if (file.isDirectory()) {
                System.out.println(file.getPath());
                if (file.getPath()
                        .equals("C:\\Users\\Desktop\\Application\\folder 2")) {
                    System.out.println("got folder");
                                            FileUtils.deleteDirectory(file);
                } else {
                    System.out.println("Didn't get it.");

                }
            }
        }
输出

C:\Users\U201165\Desktop\Newfolder\Newfolder(2)
got folder
C:\Users\U201165\Desktop\Newfolder\Newfolder(3)
Didn't get it.
C:\Users\U201165\Desktop\Newfolder\Newfolder(4)
Didn't get it.
C:\Users\U201165\Desktop\Newfolder\Newfolder(5)
Didn't get it.

System.out显示文件对象,但在equals方法中使用的是file.getPath()。将file.getPath()放入system.out中,以便查看实际比较的内容。File对象有一个类似getExplicitPath()方法的方法。您可能需要使用该路径,而不是路径。在某些情况下,我认为path只显示文件名,或者相对路径不是预期的路径。

您可能从
getPath()
获得相对路径。要确保从
文件
对象获取绝对路径,请使用:

if (file.getAbsolutePath().equals("C:\\Users\\Desktop\\Application\\folder2")) ...
而不是
getPath()