Java中如何删除目录内容?

发布于 2024-12-10 01:10:26 字数 145 浏览 0 评论 0 原文

枚举目录后,我现在需要删除所有文件。

我用:

final File[] files = outputFolder.listFiles();
files.delete();

但这并没有删除目录。

After enumerating a directory, I now need to delete all the files.

I used:

final File[] files = outputFolder.listFiles();
files.delete();

But this hasn't deleted the directory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

旧话新听 2024-12-17 01:10:26

您必须对每个文件执行此操作:

public static void deleteFolder(File folder) {
    File[] files = folder.listFiles();
    if(files!=null) { //some JVMs return null for empty dirs
        for(File f: files) {
            if(f.isDirectory()) {
                deleteFolder(f);
            } else {
                f.delete();
            }
        }
    }
    folder.delete();
}

然后调用

deleteFolder(outputFolder);

You have to do this for each File:

public static void deleteFolder(File folder) {
    File[] files = folder.listFiles();
    if(files!=null) { //some JVMs return null for empty dirs
        for(File f: files) {
            if(f.isDirectory()) {
                deleteFolder(f);
            } else {
                f.delete();
            }
        }
    }
    folder.delete();
}

Then call

deleteFolder(outputFolder);
半葬歌 2024-12-17 01:10:26

要删除包含文件的文件夹,无需循环或递归搜索。您可以直接使用:

FileUtils.deleteDirectory(<File object of directory>);

该功能将目录删除该文件夹及其中的所有文件。

To delete folder having files, no need of loops or recursive search. You can directly use:

FileUtils.deleteDirectory(<File object of directory>);

This function will directory delete the folder and all files in it.

┊风居住的梦幻卍 2024-12-17 01:10:26

删除目录之前必须先删除该目录中的所有文件。

有些第三方库具有许多常用实用程序,包括为您执行此操作的库:

All files must be delete from the directory before it is deleted.

There are third party libraries that have a lot of common utilities, including ones that does that for you:

对你的占有欲 2024-12-17 01:10:26

您无法删除数组!这应该工作得更好:

for (File f : files) f.delete();

但如果文件夹不为空,它就不起作用。对于这种情况,您将需要递归地下降到文件夹层次结构并删除所有内容。是的,遗憾的是 Java 默认情况下无法做到这一点......

You can't delete on an array ! This should work better :

for (File f : files) f.delete();

But it won't work if the folders are not empty. For this cases, you will need to recursively descend into the folder hierarchy and delete everything. Yes it's a shame Java can't do that by default...

感情废物 2024-12-17 01:10:26

这是在没有库的情况下解决问题的一种可能的解决方案:

public static boolean delete(File file) {

    File[] flist = null;

    if(file == null){
        return false;
    }

    if (file.isFile()) {
        return file.delete();
    }

    if (!file.isDirectory()) {
        return false;
    }

    flist = file.listFiles();
    if (flist != null && flist.length > 0) {
        for (File f : flist) {
            if (!delete(f)) {
                return false;
            }
        }
    }

    return file.delete();
}

Here is one possible solution to solve the problem without a library :

public static boolean delete(File file) {

    File[] flist = null;

    if(file == null){
        return false;
    }

    if (file.isFile()) {
        return file.delete();
    }

    if (!file.isDirectory()) {
        return false;
    }

    flist = file.listFiles();
    if (flist != null && flist.length > 0) {
        for (File f : flist) {
            if (!delete(f)) {
                return false;
            }
        }
    }

    return file.delete();
}
岛徒 2024-12-17 01:10:26

您无法删除 File 数组。正如所有其他答案所建议的那样,您必须在删除文件夹之前删除每个单独的文件......

final File[] files = outputFolder.listFiles();
for (File f: files) f.delete();
outputFolder.delete();

You can't delete an File array. As all of the other answers suggest, you must delete each individual file before deleting the folder...

final File[] files = outputFolder.listFiles();
for (File f: files) f.delete();
outputFolder.delete();
隔岸观火 2024-12-17 01:10:26

将 FileUtils 与 FileUtils.deleteDirectory() 一起使用;

Use FileUtils with FileUtils.deleteDirectory();

御守 2024-12-17 01:10:26
for(File f : files) {
    f.delete();
}    
files.delete(); // will work
for(File f : files) {
    f.delete();
}    
files.delete(); // will work
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文