How to delete all files and folders in one folder on Android

前端 未结 12 1026
广开言路
广开言路 2021-01-30 17:52

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 < F         


        
12条回答
  •  再見小時候
    2021-01-30 18:18

    For your case, this works perfectly http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#cleanDirectory(java.io.File)

    File dir = new File("dir_path");
    if(dir.exists() && dir.isDirectory()) {
        FileUtils.cleanDirectory(dir);
    }
    

    If you wanna delete the folder itself. (It does not have to be empty). Can be used for files too.

    http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#forceDelete(java.io.File)

    File dir = new File("dir_path");
    if(dir.exists()) {
        FileUtils.forceDelete(dir);
    }
    

提交回复
热议问题