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
java.io.File.createTempFile(prefix, suffix);
Let the temp file management for that operating system determine the policy for destroying files.
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.
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.
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.
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.
精彩评论