Hector Barbossa
Hector Barbossa

Reputation: 5528

Alternatives to deleteOnExit

I want to delete some files after a certain a time say once every day.Is using deleteOnExit() for this a good option ? Any other suggestions ?

I have some flash content which render its state by reading from some xml files stored inside web server root.These xmls are created on the fly.Now I want to delete these files.It would be better if I can manage this using java

Upvotes: 0

Views: 3821

Answers (5)

mbarlocker
mbarlocker

Reputation: 1386

Definitely avoid File.deleteOnExit. I had an issue where I was calling that multiple times per API call. Basically, it appends the file to a list of files to clean up on exit. The JVM never exited, since it was running in a web context. So, I had a memory leak of a bunch of files hanging around forever. It's much better to set up a cronjob or just delete the file after you're done with it.

Upvotes: 1

Isaac Truett
Isaac Truett

Reputation: 8874

Consider using Quartz to schedule operations in Java. You could either scan the directory for files older than 24 hours on a recurring schedule, or create a new job for each file that runs 24 hours later.

Upvotes: 0

Sasha O
Sasha O

Reputation: 3749

The problem with deleteOnExit() is that if your application crashes the files are left forever. I would run a thread to clean the temp directory (except for the open files) periodically.

Upvotes: 0

Edwin Buck
Edwin Buck

Reputation: 70939

java.io.File.createTempFile(prefix, suffix);

Let the temp file management for that operating system determine the policy for destroying files.

Upvotes: 3

corsiKa
corsiKa

Reputation: 82579

Personally, I would write a script that goes through your directory to delete files that meet your criteria (24 hours old, for instance) and run it via a cron job. I would probably have it run at a time when server load is lowest.

Upvotes: 2

Related Questions