How to delete files in java when find according to older then N days and file type
how to delete files in java when we want to use old, and file type in an folder
please give the complete code
Java
- asked 7 years ago
- Sunny Solu
1Answer
public void remove_old_files(String fiile_path) {
try {
File directory = new File(fiile_path);
if (directory.exists()) {
File[] listFiles = directory.listFiles();
Date today = new Date();
long purgeTime1 = today.getTime();
long backtime = new Long("2592000000");//2592000000
long purgeTime = (purgeTime1 - backtime);
for (File listFile : listFiles) {
if (listFile.lastModified() < purgeTime) {
if (listFile.getName().endsWith(".dat")) {
if (!listFile.delete()) {
System.err.println("Unable to delete file: " + listFile);
}
}
}
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.toString());
}
}
Call to function
remove_old_files("d:/datafolder");
- answered 7 years ago
- Community wiki
Your Answer