按上次修改日期获取文件

时间:2013-04-24 13:41:25

标签: java file directory

通过上次修改日期获取文件的最快方法是什么? 我有一个包含一些txt文件的目录。用户可以按日期进行研究,我按照lastmodified date(在File []中)列出目录中的所有文件,然后搜索具有特定日期的正确文件。 我使用上次修改日期的集合排序来对我的文件进行排序。当我从本地驱动器获取文件时速度很快但是当我想访问网络(专用网络)上的驱动器时,获取文件大约需要10分钟。我知道我不能比网络更快但是有一个解决方案可以比我的解决方案更快吗?

例如我的代码:

File[] files = repertoire.listFiles();         

Arrays.sort(files, new Comparator<File>() {
    @Override
    public int compare(File o1, File o2) {
        return Long.valueOf(o2.lastModified()).compareTo(o1.lastModified());
    }
});

for (File element : files) {
    // i get the right file;
}

感谢您的帮助

以下是解决方案:

Path repertoiry = Paths.get(repertoire.getAbsolutePath());
final DirectoryStream<Path> stream = Files.newDirectoryStream(repertoiry, new DirectoryStream.Filter<Path>() {
     @Override
     public boolean accept(Path entry) throws IOException {
          return Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis() >= (dateRechercheeA.getTime() - (24 * 60 * 60 * 1000)) && Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis() <= (dateRechercheeB.getTime() + (24 * 60 * 60 * 1000));
     }
});
for (Path path : stream) {
     if (!path.toFile().getName().endsWith("TAM") && !path.toFile().getName().endsWith("RAM")) {
         listFichiers.add(path.toFile());
     }
}

1 个答案:

答案 0 :(得分:3)

使用java 7 NIO包,可以过滤目录以仅列出所需的文件 (警告DirectoryStreams不会遍历子目录。)

Path repertoire = Paths.get("[repertoire]");
try ( DirectoryStream<Path> stream = Files.newDirectoryStream(repertoire, new DirectoryStream.Filter<Path>() {
        @Override
        public boolean accept(Path entry) throws IOException {
            return Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis() = [DATE_SEARCHED (long)] 
        }
 })){

     for (Path path : stream) {
          // Path filtered...
     }
 }

通常,此解决方案提供比创建完整文件列表更好的性能,对列表进行排序,然后迭代列表以找到正确的日期。

使用您的代码:

//use final keyword to permit access in the filter.
final Date dateRechercheeA = new Date();
final Date dateRechercheeB = new Date();

Path repertoire = Paths.get("[repertoire]");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(repertoire, new DirectoryStream.Filter<Path>() {
    @Override
    public boolean accept(Path entry) throws IOException {
        long entryDateDays = Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).to(TimeUnit.DAYS);
        return !entry.getFileName().endsWith("TAM") //does not take TAM file
                && !entry.getFileName().endsWith("RAM") //does not take RAM file
                && ((dateRechercheeB == null && Math.abs(entryDateDays - TimeUnit.DAYS.toDays(dateRechercheeA.getTime())) <= 1)
                || (dateRechercheeB != null && entryDateDays >= (TimeUnit.DAYS.toDays(dateRechercheeA.getTime()) - 1) && entryDateDays <= (TimeUnit.DAYS.toDays(dateRechercheeA.getTime()) + 1)));
    }
})) {
    Iterator<Path> it = stream.iterator();
    //Iterate good file...

}

过滤器直接在accept方法中生成,而不是在。

之后

JAVA SE 7 - Files - newDirectoryStream()

相关问题