0


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 the files at 11:10 and delete those from before 11:00? How to delete just those files? Please can anyone help me?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sri
  • 669
  • 2
  • 13
  • 25
  • 3
    I'd say RTFM: http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html – Romain Mar 17 '11 at 08:51
  • 2
    Romain: at least link to the current version: http://download.oracle.com/javase/6/docs/api/java/io/File.html (otherwise Google will keep thinking that Java 1.4.2 is still interesting). – Joachim Sauer Mar 17 '11 at 09:22

2 Answers2

2

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.

dogbane
  • 266,786
  • 75
  • 396
  • 414
1

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

Patric
  • 2,789
  • 9
  • 33
  • 60