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 Removing temporary files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Removing temporary files" Watch "Removing temporary files" New topic

Removing temporary files

K Gupta
Ranch Hand

Joined: Aug 31, 2004
Posts: 43
I don't know if I am posting at right place, so my apologies if its not

I have a problem regarding removing my temporary files once they are sendt to or viewed by the client.Now I can't call delete afetr sending because I don't know till when the user clicks the save button.So I need some way to figure that out .Please help.

Thax in advance.

If I am destined to fail, then I do have a purpose in my life: To fail my destiny.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

What sort of application is this? A Swing app? A Web app?

If it's a Swing app, mark the files as temporary files -- look at the class for this. Marked files will be deleted automatically when the JVM exits.

If it's a Web app, you can't delete files on the user's machine.

[Jess in Action][AskingGoodQuestions]
K Gupta
Ranch Hand

Joined: Aug 31, 2004
Posts: 43
Its a web -app and I want to delete file from the server after user has downloaded it
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
I would use a servlet in my web app to provide the file, after the file is viewed the servlet will delete it. Lets say you generate a timestamp.pdf file on the server in an specific place. You will give the user a link to the servlet and pass the servlet the timestamp (or file id) via a parameter. Then the servlet will get the file, open it with a inputstream and print it to the response.outputstream. At the end you flush the outputstream and delete the file.

This will clean up your temp files. But user can only download them once. If they try a second time they will cause an exception cause the file was deleted at the end of the first use. If this is a problem tell me, there is a second solution to the problem but maybe it will need some changes to your app.

She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
I agree. Here's the link:
subject: Removing temporary files
It's not a secret anymore!