Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Force Delete of files from a folder

 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
isha krishnan
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 155
Java Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic