如何在Android上删除一个文件夹中的所有文件和文件夹 [英] How to delete all files and folders in one folder on Android

查看:1090
本文介绍了如何在Android上删除一个文件夹中的所有文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码删除所有文件:

I use this code to delete all files:

File root = new File("root path");
File[] Files = root.listFiles();
if(Files != null) {
    int j;
    for(j = 0; j < Files.length; j++) {
        System.out.println(Files[j].getAbsolutePath());
        System.out.println(Files[j].delete());
    }
}

它将删除false,其中Files[j]是文件夹.

It will delete false where Files[j] is a folder.

我要删除文件夹及其所有子文件.
我该如何修改?

I want to delete folder and all its sub files.
How can I modify this?

推荐答案

最简单的方法是使用

Simplest way would be to use FileUtils.deleteDirectory from the Apache Commons IO library.

File dir = new File("root path");
FileUtils.deleteDirectory(dir);

请记住,这也会删除包含目录.

Bear in mind this will also delete the containing directory.

在gradle文件中添加此行以使用Apache

Add this line in gradle file to have Apache

compile 'org.apache.commons:commons-io:1.3.2'  

这篇关于如何在Android上删除一个文件夹中的所有文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆