开发者

File deletion in Java

Every 5 minutes in a thread I create new files and store them into a folder.

Every day at 11:10 A.M., I have to delete the old files. However, one condition is that to be deleted a file must have been created before this 11:00 A.M. Files created after 11:00 should not be deleted. How can I list th开发者_如何学Ce files at 11:10 and delete those from before 11:00? How to delete just those files? Please can anyone help me?


There are various methods available in the File class which can help.

  • To list the files in a directory use the listFiles method. This will return an array of Files which you can iterate over.
  • To check when a file was last modified use the lastModified method.
  • To delete a file use the delete method.

You also need to work out the value of 11:10am so that it can be compared to the file's last modified time. You can use the Calendar class for this.


First you should create a cronjob or a scheduled task that runs your java application at arround 11:10.

For determining if the file needs to be deleted check out the API of "File" (e.g. "lastModified()" and "delete()": http://download.oracle.com/javase/6/docs/api/java/io/File.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜