6 Replies Latest reply: Aug 9, 2008 4:51 PM by 807589 RSS

    Unable to delete a file

    807589
      BufferedInputStream bis=new BufferedInputStream(new FileInputStream(fpara2));
      File fbis=new File(fpara2);
      System.out.println("fbis.getAbsolutePath:"+fbis.getAbsolutePath());
      
      bos=new BufferedOutputStream(new FileOutputStream(f)); 
           int o;
                               do
                                    {
                                    o=bis.read();
                                         bos.write(o); 
                                    }
                                    while (o!=-1);
                                    
                                    if (o==-1)
                                    { 
                                         System.out.println("End of file reached");
                                    }
                                                             
                                    boolean del=fbis.delete();
                                    System.out.println("DELETE:"+del);
                                    
                                    bos.close();
                                    bis.close();
      --- The file is not deleted. The console shows DELETE:false.
        • 1. Re: Unable to delete a file
          807589
          .
          • 2. Re: Unable to delete a file
            807589
            you've checked the path and know that the file exists? and if it exists, you know it isn't read-only?
            • 3. Re: Unable to delete a file
              807589
              You can't delete the file until you have closed the stream attached to it.
              • 4. Re: Unable to delete a file
                807589
                As you can see in many threads, people talks about a bug in some JVM implementations preventing files to be deleted even if you have closed any stream related to it. I tried many times to delete files to no success until I read some posts advising to use the System.gc() method to destroy any object previously closed (but presumably still existing).

                I tried

                ...
                System.gc();
                for(File iterator: arrayOfFiles)
                iterator.delete();
                ...

                but the result I obtained wasn't totally satisfactory, because the first files from the array of files weren't delete while the last ones were. So I thought that maybe the problem is the garbage collector had no time to "act" before the first files were deleted. My (not quite elegant, but effective) solution was to introduce a delay of 500ms between the garbage collector request and the first delete statement. I know you have no warranty about the garbage collector acting when you ask it to, but... Anyway, this is my code:

                ...
                System.gc();

                Thread.sleep(500); // delay to allow the garbage collector to do its work before any deletion is attempted

                for(File iterator: arrayOfFiles)
                iterator.delete();
                ...


                As I could see, it works 100% the times I tested it.

                I don't know if this "solution" was mentioned before or not but, in either case, I hope this helps someone.

                Regards
                • 5. Re: Unable to delete a file
                  807589
                  Is there a reason why you are digging up every file IO question ever asked on this forum and adding your generic reply?

                  We only need to see your solution to a specific problem once, not several times in similar threads.

                  The best way I can think of to induce the garbage collector to work is to call a System.gc() and Thread.sleep every active thread so that it gets priority. Obviously no guarantee, but 10 seconds should be a good place to start working back from.
                  • 6. Re: Unable to delete a file
                    807589
                    No. There cannot be a reason for something that didn't happen, so please stop accusing me of a thing I didn't do. I just added my "generic" reply to 2 (maximum 3) questions, everyone of them asking about the same thing because I thought people having that problem would have more probabilities to read it. So, as you can see (in case that you do want to see and not just pay you bad mood with others), it was quite an specific reply, more than generic. Anyway, thanks for your warm welcome to my first post.

                    To everyone else, please forgive having to read arguments instead of good Java issues. I just hope it helps someone. I'm more a beginner than an expert, but always good-intended.

                    Regards