wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Unable to Delete Folder + I can't belive i m posting this :( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Unable to Delete Folder + I can Watch "Unable to Delete Folder + I can New topic
Author

Unable to Delete Folder + I can't belive i m posting this :(

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 753
Hey,

I have a simple program for which will create one folder,
copy some file from another folder to the newly created folder,
compress the newly created folder and delete the newly created folder which had been compressed.

My program is creating new folder compressing it, but not able to delete it.



and if i hard code the path like bellow and put it on the top of the main method it deletes the folder. fine.

same line if i move to the end of the method it does not delete folder.

What could be the possible reason for it. i m not able to understand it.



Jigar Naik


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Don't know, but it doesn't look like a beginner's topic. The IO forum looks a good place to move it, because they discuss files there.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You don't ever actually close the files you're reading from. You set "in" to null, but you don't ever call in.close(). On Windows, an open file cannot be deleted. I also don't see your deleteDir() routine, but hopefully it deletes all the files in a folder before deleting the folder, as Windows, again, won't delete a folder with files in it.


[Jess in Action][AskingGoodQuestions]
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 753
I don't understand what mistake i am making.

deleteDir() is there in the main method at second last line and in a classs it is last method.

bellow is my modified compressFolder() method.



Now i am closing the in and out both the stream.

the deleteDir() is recursively called.. The unit test for the deleteDir() metho dis pass.

Something is wrong in the compressFolder() method. After execution of compressFolder() method deleteDir() method does not delete folder.

With this new compressFolder() method in which i am taking care of closing all the streams, its deleting only one file from the folder.

logically it should delete all the files one by one and then it should delete the folder.

bellow is the stack trace

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 753
finally i got it.

i was calling in.close() in finally block that means it was closing only last file.

Bellow is the method which is closing all the streams properly.


Thanks a lot all of you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to Delete Folder + I can't belive i m posting this :(
 
Similar Threads
Cant delete all files in a directory in servlet in NetBeans
files are not delete
delete files in a driectory
Retrieve Folder and File Information
how to put two different folders(which is in different location) into one zip entry