Results 1 to 12 of 12
  1. #1
    Jovaras is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default recursive directories deletion leaves random files

    Java Code:
        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;
                }
            }
        } 
        return dir.delete();
    }
    Here, take a look at this code.
    I call this method with directory, which has subdirectories.

    Now, every time I try it, it simply does not delete random folder.
    Like it would be executed too fast.
    What could be the problem with this code?
    Thanks in advance.
    P.S. sorry for bad English.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    A suggestion for debugging your code, print out the name of the file to be deleted instead of actually deleting it.

    Your code works for me.
    Last edited by Norm; 05-24-2011 at 04:10 PM.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Why are you creating new File objects from the directory object?
    There is a method in File that will give you an array of Files back, not Strings...

  4. #4
    Jovaras is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    I tried it. It states it deleted them all, still they are there..

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Then show that code, because that is the correct way of doing this, not creating new File objects liek you currently are doing.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Are there any OS settings for the files (R/O for example) that would prevent a delete working properly?

  7. #7
    Jovaras is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    No, it's random.

  8. #8
    Jovaras is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    That bug seems to be around closing a File but its handle not being released immediately, thus any attempt to rename that File prior to the finaliser being run by the garbage collector will result in a Windows system saying (correctly) that the file is open.

    Now, it could be a similar thing, except you are not getting a false back from the delete() call.

    What version of Java are you using?

  10. #10
    Jovaras is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    1.6.0.23
    Sorry if you didn't get me right, but I'm getting false back from my mehod.

    However, I managed to overcome my problem, tho not in a very efficient way. I used a loop to call deleteDir() while return is false. I've also placed a counter, to track how many times it loops. Sometimes it goes over 200 and sometimes it doesn't need to loop at all.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    That bug seems to be around closing a File but its handle not being released immediately
    And many people have had success with explicitly nulling any variable references to a File that is to be deleted, and calling System.gc() before attempting to delete.

    db

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    But that seems to be to do with other File references, doesn't it?

    For example, this should work:
    Java Code:
    File f = new File ("something");
    f.delete();
    since there are no other File objects hanging around.

    Now if the OP does:
    Java Code:
    File dir = ...
    File[] files = dir.listFiles();
    for (File f : files) {
        f.delete(); // presuming they're not directories.
    }
    That should always work, surely since there are no other things pointing at those files?

    Now, the OP is creating a whole load of new File objects which I would argue (not knowing what else is happening in their system) is the root of their problem...or am I barking up the wrong tree?

Similar Threads

  1. Searching directories for folders and .txt files
    By XDrew574X in forum New To Java
    Replies: 1
    Last Post: 03-29-2011, 09:41 PM
  2. Save files in directories
    By Tota in forum New To Java
    Replies: 2
    Last Post: 09-02-2010, 02:49 PM
  3. deletion of files by file type
    By robby14 in forum Advanced Java
    Replies: 1
    Last Post: 05-03-2010, 08:42 AM
  4. List of files and directories.
    By artemff in forum CLDC and MIDP
    Replies: 6
    Last Post: 01-02-2010, 04:06 PM
  5. Random Access Files concept
    By AralX in forum New To Java
    Replies: 2
    Last Post: 12-25-2007, 07:04 PM

Posting Permissions

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