如何在目录中搜索带有re的文件?

发布于 2024-11-02 19:02:20 字数 175 浏览 4 评论 0原文

我想在目录中搜索文件。文件名包含一些字符串,例如 00012345,而完整名称应为 XXXXXX_00012345_XXXXXX.PDF。

我是否需要循环遍历目录中所有文件的文件名来比较它们的名称?我的目录有很多文件,我不想这样做。

加: 该目录包含数百万个文件,所有文件都是pdf。 我每次只需要一个。

I want to search for a file inside a directory. The filename contain some string such as 00012345, while the complete name should be XXXXXX_00012345_XXXXXX.PDF.

Do I need to loop through the directory for the filename of all the files to compare their name? My directory has A LOT of file, I don't want to do it this way.

Plus:
The directory contain millions of file, all of them are pdf.
I only need one at each time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

千笙结 2024-11-09 19:02:20

您可以将这项工作委托给 java.io.File 类,它有一些方便的方法可以从目录中返回符合某些条件的文件列表。

在您的情况下,我将实现一个 FilenameFilter 来测试文件名是否符合您的条件并使用此方法:

File[] files = directory.listFiles(new FileNameFilter() {
   @Override
   public boolean accept(File dir, String name) {
      return nameMatchesCriteria();  // to be implemented...
   }
});

directory 作为 File > 对象。

You can delegate this job to the java.io.File class, it has some convenience methods to return lists of files from directories, that match some criteria.

In your case, I'd implement a FilenameFilter, that tests, if a filename matches you criteria and use this method:

File[] files = directory.listFiles(new FileNameFilter() {
   @Override
   public boolean accept(File dir, String name) {
      return nameMatchesCriteria();  // to be implemented...
   }
});

with directory as an File object.

你げ笑在眉眼 2024-11-09 19:02:20

正如其他答案中所述,有各种方便的方法/库可以帮助您做到这一点。然而,在某种程度上它们都需要读取所有目录条目,并将名称与您的模式进行比较。

为什么?

因为这是典型操作系统提供的用于执行此类操作的 API。如果您有数百万个文件名需要检查,这将不可避免地很慢。


如果您想快速完成此类操作,则需要重新设计系统,以便文件空间具有某种(目录)结构,以便您可以导航到所需的文件而不是搜索。我不知道这对于您的应用是否可行。

There are various convenience methods / libraries to help you do this, as described in other answers. However at some level they all entail reading all of the directory entries, and comparing the names with your pattern.

Why?

Because that is the API that a typical operating system provides for doing this kind of thing. If you have millions of filenames to check, this will inevitably be slow.


If you want to do this kind of thing quickly, you will need to redesign your system so that you have some (directory) structure to the filespace so that you can navigate to the file you need rather than searching. I don't know if this would be feasible for your application.

伴梦长久 2024-11-09 19:02:20

实现 java.io.FilenameFilter 检查正确的扩展名 (String.endsWith()) 以及是否存在其他目标字符串 (String.indexOf()),然后将其传递到 File.listFiles(FilenameFilter)。在本例中,File 指向感兴趣的目录。

Implement a java.io.FilenameFilter the checks for the correct extension (String.endsWith()) and the presence of the other target string (String.indexOf()) then pass that to File.listFiles(FilenameFilter). The File in this case, pointing to the directory of interest.

寄人书 2024-11-09 19:02:20

如果您需要递归搜索,则 Apache Commons-IO FileUtilslistFiles(文件目录, IOFileFilter fileFilter, IOFileFilter dirFilter )

If you require a recursive search, the Apache Commons-IO FileUtils does a great job with listFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)

冰雪梦之恋 2024-11-09 19:02:20

commons.io 的 FileUtils 提供用于递归搜索的直接 API。

FileUtils.listFiles(, , );

例如。

String extensions = {"pdf", "xml"};
bool recursiveSearch = true;
File dir = new File("<path of directory to be searched>");
Collection searchResult = FileUtils.listFiles(dir , extensions, recursiveSearch );

searchResult 将包含从给定目录到其递归深度递归搜索的所有文件集合。

FileUtils of commons.io provide direct API for recursive search..

FileUtils.listFiles(, , );

eg.

String extensions = {"pdf", "xml"};
bool recursiveSearch = true;
File dir = new File("<path of directory to be searched>");
Collection searchResult = FileUtils.listFiles(dir , extensions, recursiveSearch );

searchResult will contains all the collections of files recursively searched from the given dir to its recursive depth.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文