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

前端 未结 12 1055
广开言路
广开言路 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:16

    As a kotlin extension function you can do this

    fun File.deleteDirectoryFiles(){
        this.listFiles().forEach {
            if(it.isDirectory){
                it.deleteDirectoryFiles()
            }else{
                it.delete()
            }
        }
    
        this.delete()
    }
    

    Then you can just do

    file.deleteDirectoryFiles()
    

提交回复
热议问题