Sri
Sri

Reputation: 699

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

Upvotes: 0

Views: 537

Answers (2)

dogbane
dogbane

Reputation: 274532

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.

Upvotes: 2

Patric
Patric

Reputation: 2989

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

Upvotes: 1

Related Questions