【问题标题】:Getting files by lastmodified date按上次修改日期获取文件
【发布时间】:2013-04-24 13:41:25
【问题描述】:

在 lastmodified 日期之前获取文件的最快方法是什么? 我有一个包含一些 txt 文件的目录。用户可以按日期进行研究,我按最后修改日期(在 File[] 中)列出目录中的所有文件,然后搜索具有特定日期的正确文件。 我使用使用 lastmodified 日期的集合排序来对我的文件进行排序。当我从本地驱动器获取文件时速度很快,但是当我想访问网络(专用网络)上的驱动器时,获取文件可能需要大约 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());
     }
}

【问题讨论】:

  • 获得更快的网络。
  • 是的,但它是一个公司网络。这些文件在服务器上,当我在同一栋建筑物中访问此服务器时,它非常快(例如每个文件 0.11 秒),但是当我在其他城市访问它们时,请求时间约为 1.30 分钟..

标签: java file directory


【解决方案1】:

使用 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()

【讨论】:

  • 感谢您的回答。但我不知道如何实现这一点,因为当用户进行研究时,他会在 2 个日期之间进行选择,例如,他选择 24 avril 到 27 avril 之间,他得到 3 个文件。我怎样才能用你的解决方案来点缀它?我不能把我的约会放在这个地方[DATE_SEARCHED (long)]。我得到了一个具有这种格式的日期: dateA.getTime() (dateA 是一个格式为 dd/MM/yy 的日期)和 dateB.getTime() 为了更全面,我用我的整个代码更新了我的帖子。
  • 没关系,我想通了。现在我需要测试它是否更快。
  • 好吧,我在网络上测试了它,它运行良好。非常感谢 !我将更新我的帖子以提供解决方案。再次感谢
  • 不要在过滤器中直接制作TAM/RAM文件的过滤器。并且不要忘记添加 try(...){ } 以正确关闭 DirectoryStream 资源,就像我的修改一样。
  • 谢谢,但有一件事 entry.getFileName().endswith("TAM") 不起作用你需要使用 entry.toFile().getName() 否则它不会结束路径。
猜你喜欢
  • 2020-09-30
  • 1970-01-01
  • 2011-03-21
  • 1970-01-01
  • 2021-10-08
  • 2017-01-02
  • 2011-10-15
  • 2011-07-12
  • 2015-02-19
相关资源
最近更新 更多