Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2010
    Rep Power

    Default wait until directory is completely deleted


    I have a directory that contains a lot of files. I want to delete the entire directory as well as all the files in it.

    I want my code to wait until every File in that directory (including the directory itself) is deleted before the next command is executed.

    Here is what I have so far:

    deleteDir(new File(path_to_dir)); //custom made method deletes dir and files in it
    while((new File(path_to_dir)).exists()) {
    //continue executing other commands

    Do you think that this while loop will make sure that all files and the dir are deleted before the next command is executed?

    Other (more elegant/secure) solutions to this?

    thanks in advance,


  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Cebu City, Philippines
    Rep Power


    in the given example,

    the while loop is useless, because you are invoking a method which will not return unless the task inside of that method is done or if that method is executing a thread that will remove all files on a specific folder.

    The risk if you force to do it that way is most of the CPU consumption will be from your program.
    Other applications will slowdown their performance(slightly on quadcore processors). I don't know if you are aware of it.

    a suggestion:

    Instead, let thread1 execute the delete method and put some sleep to it. and execute another thread2 that checks the directory every 5 to 10 milliseconds, when the directory is empty, do the desired commands also in thread2.
    Last edited by sukatoa; 03-26-2010 at 01:10 PM.
    freedom exists in the world of ideas

Similar Threads

  1. How to Recover Deleted Files from Eclipse ?
    By vinod827 in forum Eclipse
    Replies: 1
    Last Post: 08-02-2009, 12:07 PM
  2. Is JAVA completely object-oriented???
    By venkateshk in forum New To Java
    Replies: 2
    Last Post: 11-17-2008, 09:17 AM
  3. Completely new to Java problem
    By Seamo14 in forum New To Java
    Replies: 4
    Last Post: 10-09-2008, 12:50 PM
  4. Files not completely deleted on Linux
    By amamare in forum Advanced Java
    Replies: 0
    Last Post: 12-19-2007, 02:12 PM
  5. I am completely stuck
    By jpnym15 in forum New To Java
    Replies: 2
    Last Post: 11-14-2007, 06:40 PM

Tags for this Thread

Posting Permissions

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