File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How Do I completely Empty My DIrectory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How Do I completely Empty My DIrectory?" Watch "How Do I completely Empty My DIrectory?" New topic

How Do I completely Empty My DIrectory?

Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Hi guys,

Will Someone please tell me Why this code of mine is deleting some files and leavind some behind while I want the Directory to be completely Emptied!!!

My Code is

Well it is just that I thought it should clear the directory.

Give a beggar a fish; feed him for a day. Teach him how to fish; Feed him for a lifetime.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

There are different reasons why files cannot be deleted; for example (on Windows) when some process has the file open, or when the user doesn't have permission to delete the file. If these are files that you read or wrote in your own Java program, make sure that you close the file (or any input / output streams on the file) before you try to delete it.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3152

And Your directory might contain other directories in which case you would have to go into those and delete the files there in as well, if at all empty according to you is no files/directories to be present.

Mohamed Sanaulla | My Blog
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Thank you Mohamed and Jesper,

I have Closed all the inputstreams but there is no positive change.

The situation is am Having text files which are getting converted to Pdf. The pdfs are getting sent to an e-mail adress contained in the text files. The program is then supposed to delete both the pdfs and the text files. All the pdfs are getting deleted and Some of the text files. The question is now that the program is reading from all the text files and sending all the Pdfs, Why isn't it deleting all the text files but choosing which ones to delete and leaving some? I do not have other Directories in the same Directory.
I agree. Here's the link:
subject: How Do I completely Empty My DIrectory?
It's not a secret anymore!