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

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

    // Delete folder and its contents

    public static void DeleteFolder(File folder)
    {
        try
        {
            FileUtils.deleteDirectory(folder);
        } catch (Exception ex)
        {
            Log.e(" Failed delete folder: ", ex.getMessage());
        }
    }
    

    // Delete folder contents only

    public static void DeleteFolderContents(File folder)
    {
        try
        {
            FileUtils.cleanDirectory(folder);
        } catch (Exception ex)
        {
            Log.e(" Failed delete folder contents: ", ex.getMessage());
        }
    }
    

    Docs: org.apache.commons.io.FileUtils.cleanDirectory

提交回复
热议问题