public class CleanUtil { public static void deleleFilesOfDir(String dirPathName, int retainHours) { File dir = new File(dirPathName); File[] listFile = dir.listFiles(); if (listFile == null) { return; } long now = System.currentTimeMillis(); for (File file : listFile) { deleteFile(now, retainHours, file); } } private static void deleteFile(long now, int retainHours, File file) { try { BasicFileAttributes basicAttr = Files.readAttributes(file.toPath(), BasicFileAttributes.class); long fileTime = basicAttr.lastAccessTime().toMillis(); deleteOverDueFile(retainHours, file, now, fileTime); } catch (IOException | RuntimeException e) { log.warn("failed to read file attributes: {}", file.getAbsolutePath(), e); } } private static void deleteOverDueFile(int retainHours, File file, long now, long fileTime) { if (isOverDue(retainHours, now, fileTime)) { boolean isDeleted = FileUtils.deleteQuietly(file); if (!isDeleted) { log.warn("file deleted faild: {}", file.getAbsolutePath()); } } } uT
时间: 2024-01-12 13:05:37 浏览: 120
Get-Mp3-File-From-Storage
抱歉,我不太明白你想问的问题是什么,请您再具体一些。这段代码是一个 Java 工具类,用来删除指定目录下一定时间内未被访问的文件。其中,deleleFilesOfDir 方法会遍历指定目录下的所有文件,调用 deleteFile 方法删除每一个文件;deleteFile 方法会读取文件属性,判断文件是否过期并删除;deleteOverDueFile 方法用于判断文件是否过期,并删除过期的文件。
阅读全文