0

Has i am create sample application using android using File concepts,

Following steps are :

--- 1. I give the parent folder name "default"

--- 2. To find the parent folder to sub folder.

--- 3. List the sub folder file.

--- 4. Delete the Empty the sub folder

i will completed three steps,In the problem is how to find the sub folder are Empty given me one solution ?

sample code :

  File filefirst = new File("/storage/sdcard0/Parentfoldername/");
            String[] names = filefirst.list();

            for (String name : names) 
            {
                if (new File("/storage/sdcard0/Parentfoldername/" + name).isDirectory()) {

                    File directory = new File("/storage/sdcard0/Parentfoldername/" + name);
                    //get all the files from a directory
                    File[] fList = directory.listFiles();
                    for (File file : fList) {

                        if (file.isFile()) 
                        {
                       if (directory.isDirectory()) 
                          {
                              String[] children = directory.list();
                            for (int i = 0; i < children.length; i++)
                             {

                                  new File(directory, children[i]).delete();

                             }

                          }


                        }
                    }
                }
            }

This code like delete the sub_folder inside the file ,that sub_folder are deleted not folder is file but Empty sub_folder is not delete.

visual process
  • 359
  • 1
  • 3
  • 11

1 Answers1

0
public class Utils {
  /**
   * Empty and delete a folder (and subfolders).
   * @param folder
   *            folder to empty
   */
  public static void rmdir(final File folder) {
      // check if folder file is a real folder
      if (folder.isDirectory()) {
          File[] list = folder.listFiles();
          if (list != null) {
              for (int i = 0; i < list.length; i++) {
                  File tmpF = list[i];
                  if (tmpF.isDirectory()) {
                      rmdir(tmpF);
                  }
                  tmpF.delete();
              }
          }
          if (!folder.delete()) {
            System.out.println("can't delete folder : " + folder);
          }
      }
  }
}
praveen s
  • 209
  • 4
  • 11