Doesn't File.delete() delete immediately?

Discussion in 'Java' started by vandmzah@gmail.com, Jul 5, 2006.

  1. Guest

    Hi,

    I am discovering that File.delete() does not work the way I expect. I
    am not sure if what I observe is expected behavior or if not where to
    look for a problem.In my code I have a method to delete contained empty
    directories in tempDir. It looks pretty much like this:

    protected void clear() {
    File[] leftDirs = tempDir.listFiles();
    for (int i = 0; i < leftDirs.length; i++) {
    boolean deleted=leftDirs.delete();
    if(!deleted)
    System.out.println("Can't delete" + leftDirs);
    }
    leftDirs = tempDir.listFiles(); //Try to get the
    // list again
    if(leftDirs.length>0)
    System.out.println("Files not deleted!" );
    }
    I am getting from time to time the message "Files not deleted!" without
    getting the "Can't delete" + leftDirs message. (I'm running
    j2re1.4.2_08 on XP)

    In my test case I am trying to delete a single subdirectory
    directory(in context of more complex application). When I set a
    breakpoint at the second 'if' statement, the directory is never
    there.

    It looks like the delete() method returns true, and the directory is
    being deleted, but not immediately. My expectation was that if delete()
    returns true, it is guaranteed that the directory is no longer there. I
    do not want to continue without being sure that the directory is
    deleted.

    Could please someone explain?
    , Jul 5, 2006
    #1
    1. Advertising

  2. Oliver Wong Guest

    <> wrote in message
    news:...
    > Hi,
    >
    > I am discovering that File.delete() does not work the way I expect. I
    > am not sure if what I observe is expected behavior or if not where to
    > look for a problem.In my code I have a method to delete contained empty
    > directories in tempDir. It looks pretty much like this:
    >
    > protected void clear() {
    > File[] leftDirs = tempDir.listFiles();
    > for (int i = 0; i < leftDirs.length; i++) {
    > boolean deleted=leftDirs.delete();
    > if(!deleted)
    > System.out.println("Can't delete" + leftDirs);
    > }
    > leftDirs = tempDir.listFiles(); //Try to get the
    > // list again
    > if(leftDirs.length>0)
    > System.out.println("Files not deleted!" );
    > }
    > I am getting from time to time the message "Files not deleted!" without
    > getting the "Can't delete" + leftDirs message. (I'm running
    > j2re1.4.2_08 on XP)
    >
    > In my test case I am trying to delete a single subdirectory
    > directory(in context of more complex application). When I set a
    > breakpoint at the second 'if' statement, the directory is never
    > there.
    >
    > It looks like the delete() method returns true, and the directory is
    > being deleted, but not immediately. My expectation was that if delete()
    > returns true, it is guaranteed that the directory is no longer there. I
    > do not want to continue without being sure that the directory is
    > deleted.
    >
    > Could please someone explain?


    I don't see anything in the JavaDocs to deny your assumptions. Did you
    check if this also occurs in 1.5? If so, perhaps you should file a bug with
    Sun.

    - Oliver
    Oliver Wong, Jul 5, 2006
    #2
    1. Advertising

  3. I admit it's a little far-fetched, but are you sure there isn't some
    other application/process/thread writing files into this directory
    between your 2 calls to File.list() ? What is the output if you don't
    just write your error message, but the list of file names that the
    second File.list() returns?

    /philipp
    Philipp Leitner, Jul 5, 2006
    #3
  4. Ben_ Guest

    Hello,

    Actually, it would not surprise me that much, especially when you tell its
    intermittent.

    I don't know for sure, but I can easily imagine the OS returns true on
    delete() to kind of tell "it accepts the request for deleting the file", but
    does not do it immediatly for some reason (file locked by the virus
    scanner -- especially e.g. a large zip file, operations are buffered and
    delayed, etc).

    I would advise to search the Bug Database (http://bugs.sun.com/bugdatabase/)
    for known bugs (and unexpected behaviors :) ?

    For example, I see bug ID 4045014 "In Win95 JDK 1.1.1, File.delete() fails
    to delete the file from disk" closed as "not a bug".

    I didn't look further, but I would expect that looking at more stuff like
    that will tell you that file system operations are not working as one might
    expect in the first place.
    Ben_, Jul 5, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wickwack
    Replies:
    0
    Views:
    384
    Wickwack
    Jul 22, 2003
  2. =?Utf-8?B?SlQ=?=
    Replies:
    1
    Views:
    3,636
    Hansen
    Jun 29, 2011
  3. Erik Lautier

    Login doesn't "register" immediately?

    Erik Lautier, Apr 7, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    273
    Cowboy \(Gregory A. Beamer\)
    Apr 9, 2007
  4. John Kotuby
    Replies:
    2
    Views:
    677
    John Kotuby
    Apr 20, 2007
  5. Muppet

    HTML button doesn't click immediately

    Muppet, Jan 20, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    118
    Muppet
    Jan 20, 2004
Loading...

Share This Page