简体   繁体   中英

Is wildcard searching possible with Apache Commons VFS?

I'm just learning 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"?

Have a look at the

org.apache.commons.vfs2.FileSelector

You can find the following standard implementations:

AllFileSelector, FileDepthSelector, FileFilterSelector, FileTypeSelector

The FileFilter is derived from FileDepthSelector which is probably the closest to what you can get. 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. 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.

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. I found an Apache utility that does just this. SelectorUtils

This could be used alone with the suggestion in the previous answer of implementing an implementation of FileSelector

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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