4 Replies Latest reply: Dec 11, 2012 11:43 PM by MrBabakishiyev RSS

    Unable to delete a file and directory using java.

    Ajay Sharma

      I am unable to delete files and directory. I used following code. All files are getting deleted except one file and so the directory is not getting deleted.
          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));

      if (!success) {
      return false;

      // The directory is now empty so delete it
      return dir.delete();
      Code 2:
      try {
      File dir = new File(tempLocation);
      FileUtils.deleteDirectory( dir );
      } catch( IOException ioe ) {
      System.out.println( "Error deleting directory." );
      What us wrong with the code ?