2013-04-24 85 views
4

通過上次修改日期獲取文件的最快方式是什麼? 我有一個txt文件的目錄。用戶可以按日期進行研究,按last change日期(在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()); 
    } 
} 
+1

獲得更快的網絡。 – 2013-04-24 13:47:55

+0

是的,但它是一個公司網絡。這些文件在服務器上,當我在同一棟建築物中訪問此服務器時,速度非常快(如每個文件0.11s),但是當我在其他城市請求時訪問它時,大約需要1,30分鐘。 – 2013-04-25 06:52:30

回答

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... 

} 

的過濾器在接受方法直接進行,而不是之後。

JAVA SE 7 - Files - newDirectoryStream()

+0

感謝您的支持回答。但我不知道如何實現這一點,因爲當用戶做了一項研究時,他選擇了兩個日期,例如他選擇了24 avril到27 avril,他得到3個文件。我怎樣才能將它與你的解決方案進行對比 而我不能把我的日期放在這個地方'[DATE_SEARCHED(long)]'。我使用這種格式得到了一個日期:dateA.getTime()(dateA是格式爲dd/MM/yy的日期)和dateB.getTime() 爲了更全面,我用我的整個代碼更新了我的文章。 – 2013-04-25 06:43:31

+0

沒關係,我想通了。現在我需要測試它是否更快。 – 2013-04-25 07:17:00

+0

嗯,我在網絡上測試了它,它完美地工作。非常感謝 !我會更新我的帖子以提供解決方案。再次感謝 – 2013-04-25 07:56:41

相關問題