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

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

    #1

    File mFile = new File(Environment.getExternalStorageDirectory() + "/folder");
    try {
        deleteFolder(mFile);
    } catch (IOException e) {
        Toast.makeText(getContext(), "Unable to delete folder", Toast.LENGTH_SHORT).show();
    }
    
    public void deleteFolder(File folder) throws IOException {
        if (folder.isDirectory()) {
           for (File ct : folder.listFiles()){
                deleteFolder(ct);
           }
        }
        if (!folder.delete()) {
           throw new FileNotFoundException("Unable to delete: " + folder);
        }
    }
    

    #2 (Root)

    try {
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(p.getOutputStream());
        outputStream.writeBytes("rm -Rf /system/file.txt\n");
        outputStream.flush();
        p.waitFor();
        } catch (IOException | InterruptedException e) {
           Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    

提交回复
热议问题