• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic