如何在目录中搜索带有re的文件?
我想在目录中搜索文件。文件名包含一些字符串,例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将这项工作委托给 java.io.File 类,它有一些方便的方法可以从目录中返回符合某些条件的文件列表。
在您的情况下,我将实现一个
FilenameFilter
来测试文件名是否符合您的条件并使用此方法:将
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:with
directory
as anFile
object.正如其他答案中所述,有各种方便的方法/库可以帮助您做到这一点。然而,在某种程度上它们都需要读取所有目录条目,并将名称与您的模式进行比较。
为什么?
因为这是典型操作系统提供的用于执行此类操作的 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.
实现
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 toFile.listFiles(FilenameFilter)
. TheFile
in this case, pointing to the directory of interest.如果您需要递归搜索,则 Apache Commons-IO FileUtils 与 listFiles(文件目录, 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)
commons.io 的 FileUtils 提供用于递归搜索的直接 API。
FileUtils.listFiles(, , );
例如。
searchResult 将包含从给定目录到其递归深度递归搜索的所有文件集合。
FileUtils of commons.io provide direct API for recursive search..
FileUtils.listFiles(, , );
eg.
searchResult will contains all the collections of files recursively searched from the given dir to its recursive depth.