Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Unable to delete a file and directory using java.

Ajay SharmaDec 11 2012 — edited Dec 12 2012
Hi,

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 ?

Regards,
Ajay                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 9 2013
Added on Dec 11 2012
4 comments
1,462 views