Wednesday, July 25, 2012

How to delete a directory(recursively) in Android ?


Just call the below method from anyWhere in the code and just pass the file parameter address eg..

File a=new File("sdcard/example/com");
a.deleteDir(a);


This will call below method and delete the "com" directory...

public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}


OR


void recursiveDelete (File dirPath) {
String [] ls = dirPath.list ();
for (int idx = 0; idx < ls.length; idx++) {
File file = new File (dirPath, ls [idx]);
if (file.isDirectory ())
recursiveDelete (file);
file.delete ();

}
}

2 comments:

  1. Great Article If you are interested to learn online interested courses like python,DATA SCIENCE,CYBER SECURITY,WEB DEVLOPMENT,
    then check it out

    coursack
    or you can contact us
    Facebook
    Linkedin

    ReplyDelete
  2. eliteonrent
    we provide rental services for air conditioning & oil heater services
    for more information contact us

    ReplyDelete