允许通配符从父目录中搜索子目录? [英] Allow wildcards to search for subdirectories from a parent directory?
问题描述
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
public class DirectoryContents {
public static void main(String[] args) throws IOException {
File f = new File(".");
FileFilter directoryFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
File[] files = f.listFiles(directoryFilter);
for (File file : files) {
if (file.isDirectory())
System.out.print("directory:");
else
System.out.print(" file:");
System.out.println(file.getCanonicalPath());
}
}
}
我可以列出所有子目录从父目录。
但是我想在java.Is中搜索特定的子目录有什么办法?
I m able to list all the sub directories from the parent directory. But I would like to search for particular sub directory in java.Is there any way?
directory:C:\projects\workspace\testing
如何仅列出子目录(z1)而不是文件? (子目录z1存在于各个子目录中)
how to list only subdirectories (z1) and not files? (Sub directory z1 is present in various sub directories)
directory:C:\projects\workspace\testing\z1
directory:C:\projects\workspace\testing\f5\z1
directory:C:\projects\workspace\testing\f5\a\g\h\d
输出应为包含z1的目录
The Output should be directories containing z1
推荐答案
Java 7使这种类型的东西容易,具有很大的灵活性。我将使用Java 7 PathMatcher 使用glob模式(如果需要复杂性,则使用正则表达式),并在 FileVistor 。然后通过 Files.walkFileTree()。
Java 7 made this type of thing easy with lots of flexibility. I would use a Java 7 PathMatcher with a glob pattern (or a regex pattern if the sophistication is needed) and use it in a FileVistor. Then just walk the file tree via Files.walkFileTree().
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class StackOverflow20443793
{
//Our FileVisitor Implementation. We extend SimpleFileVisitor to save work
protected class Finder extends SimpleFileVisitor<Path>
{
private final PathMatcher matcher;
Finder(String pattern)
{
matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
}
void find(Path path)
{
Path name = path.getFileName();
if ((name != null) && matcher.matches(name))
{
processDirectory(path);
}
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException
{
//Since we only care about directories, we only override
// the preVisitDirectory method. The super does null checks for us
super.preVisitDirectory(dir, attrs);
find(dir);
return FileVisitResult.CONTINUE;
}
}
private void findAndProcessSubDirectories(Path startingDirectory, String pattern) throws IOException
{
Finder finder = new Finder(pattern);
Files.walkFileTree(startingDirectory, finder);
}
private void processDirectory(Path dir)
{
System.out.println(dir);
}
public static void main(String... args) throws Exception
{
StackOverflow20443793 runner = new StackOverflow20443793();
//Find all directories in path named "z1"
runner.findAndProcessSubDirectories(Paths.get("P:\\tmp\\findExample"), "z1");
System.out.println("=====");
//Find all directories in path named "z*"
runner.findAndProcessSubDirectories(Paths.get("P:\\tmp\\findExample"), "z*");
System.exit(0);
}
}
输出:
C:\projects\workspace\testing\f5\a\g\h\d\z1
C:\projects\workspace\testing\f5\z1
C:\projects\workspace\testing\z1
=====
C:\projects\workspace\testing\f5\a\g\h\d\z1
C:\projects\workspace\testing\f5\z1
C:\projects\workspace\testing\z1
C:\projects\workspace\testing\z2
有关更多信息和示例,请参阅 http://docs.oracle.com/javase/tutorial/essential/io/find.html 和我链接的JavaDocs。
For more info and examples, see http://docs.oracle.com/javase/tutorial/essential/io/find.html and the JavaDocs I linked.
这篇关于允许通配符从父目录中搜索子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!