This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Servlets and the fly likes Delete genterated Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Delete genterated Files" Watch "Delete genterated Files" New topic

Delete genterated Files

T Williams

Joined: Sep 24, 2008
Posts: 4
In my servlet, I am creating files at runtime. These files are stored in the root directory of my servlet container.

I need to clean these files up at some time. I have created a thread to do this within my servlet.

However, I use the myfile.delete() method. The file is not deleted.
I tested the file object for canWrite()...returns false.

What is the best way (if at all) to delete these files?

thanks very much!

Venkata Kumar
Ranch Hand

Joined: Apr 16, 2008
Posts: 110

you may start a thread in init function of servlet to check and delete the files. Close the file stream properly after writing the content in file.

SCJP 5.0, SCWCD 5, preparing for SCDJWS
T Williams

Joined: Sep 24, 2008
Posts: 4
Thanks but that did not work. I got the same error.

The logic is I read through the directory and create a file object
for any file that meets certain criteria for deletion.

I then call the delete() method of the file object which of course
fails to delete the file.

I can delete files from my servlet that are NOT in the container. I'm
assuming there is either a permission issue or tomcat won't allow it.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13018
Tomcat does not normally lock files in any way.

I am betting that either some process still has those files open or this is a permission problem. How was Tomcat installed - service or user application?

I agree. Here's the link:
subject: Delete genterated Files
It's not a secret anymore!