Как узнать, когда был создан файл с помощью java, поскольку я хочу удалить файлы старше определенного периода времени, в настоящее время я удаляю все файлы в каталоге, но это не идеально:
public void DeleteFiles() {
File file = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/");
System.out.println("Called deleteFiles");
DeleteFiles(file);
File file2 = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/");
DeleteFilesNonPdf(file2);
}
public void DeleteFiles(File file) {
System.out.println("Now will search folders and delete files,");
if (file.isDirectory()) {
for (File f : file.listFiles()) {
DeleteFiles(f);
}
} else {
file.delete();
}
}
Выше мой текущий код, теперь я пытаюсь добавить оператор if, который будет удалять файлы старше, чем в неделю.
EDIT:
@ViewScoped
@ManagedBean
public class Delete {
public void DeleteFiles() {
File file = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/");
System.out.println("Called deleteFiles");
DeleteFiles(file);
File file2 = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/");
DeleteFilesNonPdf(file2);
}
public void DeleteFiles(File file) {
System.out.println("Now will search folders and delete files,");
if (file.isDirectory()) {
System.out.println("Date Modified : " + file.lastModified());
for (File f : file.listFiles()) {
DeleteFiles(f);
}
} else {
file.delete();
}
}
Добавление цикла теперь.
ИЗМЕНИТЬ
Я заметил, что при тестировании кода выше я получил последнее изменение:
INFO: Date Modified : 1361635382096
Как мне закодировать цикл if, чтобы сказать, если он старше 7 дней, удалите его, когда он находится в вышеуказанном формате?