简体   繁体   English

Apache Commons VFS是否可以进行通配符搜索?

[英]Is wildcard searching possible with Apache Commons VFS?

I'm just learning Apache Commons VFS. 我正在学习Apache Commons VFS。 I'd like to search for a file in a certain directory, but I don't know the exact name of the file. 我想在某个目录中搜索文件,但是我不知道文件的确切名称。 I do, however, know a part of the name. 但是,我确实知道名称的一部分。

To search for a file, I think I can do something like this: 要搜索文件,我我可以做这样的事情:

FileSystemManager manager = VFS.getManager();
FileObject file = manager.resolveFile(directory + "/" + filename);

if (file.exists()) {
    System.out.println("File found");
} else {
    System.out.println("File not found");
}

where "directory" is a String of the directory I want to look in, and "filename" is the exact filename of the file I want to look for. 其中“目录”是我要查找的目录的字符串,“文件名”是我要查找的文件的确切文件名。 That should print out whether or not the file is there. 那应该打印出文件是否存在。

I'm wondering if I can do something similar when I don't know the exact name of the file, but I do know part of it. 我想知道当我不知道文件的确切名称时是否可以做类似的事情,但是我确实知道其中的一部分。 For example, if I know the filename ends in "foo.txt", can I do some sort of wildcard search for "*foo.txt"? 例如,如果我知道文件名以“ foo.txt”结尾,是否可以对“ * foo.txt”进行某种通配符搜索?

Have a look at the 看看

org.apache.commons.vfs2.FileSelector

You can find the following standard implementations: 您可以找到以下标准实现:

AllFileSelector, FileDepthSelector, FileFilterSelector, FileTypeSelector AllFileSelector,FileDepthSelector,FileFilterSelector,FileTypeSelector

The FileFilter is derived from FileDepthSelector which is probably the closest to what you can get. FileFilter派生自FileDepthSelector ,它可能是最接近您可以获取的文件。 You can use this one to implement your own "wildcard" based sselector. 您可以使用此代码来实现自己的基于“通配符”的选择器。 All these Selector work like a filter. 所有这些选择器都像过滤器一样工作。 They are callbacks for the filesystemmanager when traversing the filesystem. 它们是遍历文件系统时文件系统管理器的回调。 Your filter will decide whether the file is on the selection or not. 您的过滤器将决定文件是否在所选内容上。 Deriving FileFilter from FileDepthSelector has the advantage that you can limit the directory depth level of the filesystem you go through. 从FileDepthSelector派生FileFilter的优点是您可以限制所经过的文件系统的目录深度级别。 Overall this is a nice approach because you can implement other kind of filters like filemodification time - but they could have supplied a wildcard filter which is not easy to implement if you think of filters like (*a*b.doc*) Hope this helps. 总体而言,这是一个不错的方法,因为您可以实现其他类型的过滤器,例如文件修改时间-但是它们可以提供通配符过滤器,如果您想到(* a * b.doc *)这样的过滤器,则不容易实现。

I know this question is slightly old now but I came accross it today as I am implementing an SFTP integration which needs wild card matching. 我知道这个问题现在有点老了,但是今天我遇到了这个问题,因为我正在实现需要通配符匹配的SFTP集成。 I found an Apache utility that does just this. 我找到了一个可以执行此操作的Apache实用程序。 SelectorUtils SelectorUtils

This could be used alone with the suggestion in the previous answer of implementing an implementation of FileSelector 在实现FileSelector实现的先前答案中,可以将此与建议单独使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM