aspose file tools*
The moose likes Beginning Java and the fly likes Force Delete of files from a folder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Force Delete of files from a folder" Watch "Force Delete of files from a folder" New topic
Author

Force Delete of files from a folder

Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Hi Guys I am trying to delete Files from a folder and I have tried these two methods which are Not deleting the files;
First Methos is :




The second Method is :





Both Methods Give this Exception :


Exception in thread "main" java.io.IOException: Unable to delete file: C:\ABC Statements final\Statement0.RPT at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:1390) at org.apache.commons.io.FileDeleteStrategy$ForceFileDeleteStrategy.doDelete(FileDe‌​leteStrategy.java:151) at org.apache.commons.io.FileDeleteStrategy.delete(FileDeleteStrategy.java:94) at abcbankestatement.ABCBankEStatement.main(ABCBankEStatement.java:502) Java Result: 1


Is there a Better way to force the deletion of these files??


Give a beggar a fish; feed him for a day. Teach him how to fish; Feed him for a lifetime.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1506
    
    5

Hello Stanley Mungai,

The most common reason for this to happen is - the file is locked by another process/thread. It can be part of your code(in which case, you need to check whether any part of your code is keeping the file open etc.), or you are providing that file to some other application, which is not properly closing it etc.

Please make sure that file is not locked by any other thread (apart from the thread which attempts to delete it).

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
isha krishnan
Ranch Hand

Joined: Nov 10, 2008
Posts: 50
Hi,

before running this code ,i would say:

1. "Why is there no escape character after "C:/" in ?it should be "C://"
2. this syntax i could not understand , is "Statements" and "final" name of files?
3. I don't think we can put any file names like this.
4. Just remove these name and put escape sequence after C

Then check if your code runs or not




John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Isha,
No need for escaping '/'. You need escapes while you use '\'. Also ABC Statements final should be a folder as per OP.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

also you can use apache commons-io, which is an excellent library for performing everything related to IO. it has got functions with which you can delete files forcibly. it is really good. give it a try.
isha krishnan
Ranch Hand

Joined: Nov 10, 2008
Posts: 50

yeah jai,

that name is folder name. we don't need to put escape sequence also. it works the way written.need to learn more
what Anayonkar said could be possible reason.
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Anayonkar was actually Partly right. Although I had closed all the InputStream that were reading from the Directory. the deletion was happening so soon that the bufferedReaders and the InputStream had no enough time to close. So I added this part of the code and the Deletion Now happens!!!

the edited code:





And the deletion happened. Thank you Guys for helping.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

That's really strange. You should not have to rely on waiting for garbage collection to happen for a stream to close completely.

Are you sure that you are calling close() on all streams / readers before you are trying to delete the files?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1506
    
    5

Stanley Mungai wrote:the deletion was happening so soon that the bufferedReaders and the InputStream had no enough time to close

That's very strange. Are you manually flushing and closing all streams before attempting to delete the file?

And as Jesper de Jong said, its very bad idea to have code where behavior depends on time taken by GC. What if GC chooses not to clean-up resources (because, memory usage is, say only 5%, or for some other reason)?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Force Delete of files from a folder