Results 1 to 5 of 5
  1. #1
    seandingobat is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default deleting a full directory

    Hi all,
    is there an easier way to delete a directory that has contents in it?

  2. #2
    seandingobat is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    this is my code to delete the folder and Everything inside it

    Java Code:
    public static void deletedir(File dir){
                File[] list = dir.listFiles();
                for(int x = 0 ; x < list.length; x++){
                    if(list[x].isFile()){
                        list[x].delete();
                    }else if(list[x].isDirectory()){
                        deletedir(list[x]);
                    }
                }
    
                dir.delete();
            }

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    Delete each of the elements of the directory. Do this recursively if any of the contents of the directory are, themselves, directories.

    Possibly better: check that you have permissions to delete the contents before you begin deleting them (again this will, in general, be recursive). That way if there are permissions problems you won't be left with data in a random half-deleted state.

    ---

    Just saw your second post. That looks OK.

  4. #4
    seandingobat is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    how do you check if they have permission?

    do you just check if the file has been successfully deleted ?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    That's a good question!

    If you think there might be a danger that deep within the directories is a file that you can't delete for some reason and if you really don't want to leave the directory in a half deleted state then it would be no good checking after you had deleted things.

    As far as I can see there is no method in File for checking whether you can actually delete things. Or even for checking whether a file is writable. About the only thing I can think of in such a case is to make a temporary copy and using it to restore the original in case that checking reveals that the delete failed.

Similar Threads

  1. how to get the full file name
    By priyanka3006 in forum JDBC
    Replies: 0
    Last Post: 08-05-2009, 01:55 PM
  2. Full screen test
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-24-2008, 12:24 AM
  3. Deleting an directory/subdirectory/files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:18 AM
  4. Deleting an empty directory
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:17 AM
  5. Full screen
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 06:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •