4

最終変更日でファイルを取得する最速の方法は何ですか? いくつかの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());
     }
}
4

1 に答える 1

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 - ファイル - newDirectoryStream()

于 2013-04-24T14:46:26.230 に答える