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中搜索特定的子目录吗?
directory:C:\projects\workspace\testing
如何仅列出子目录(z1)而不是文件? (子目录z1存在于各种子目录中)
directory:C:\projects\workspace\testing\z1
directory:C:\projects\workspace\testing\f5\z1
directory:C:\projects\workspace\testing\f5\a\g\h\d
输出应该是包含z1
的目录答案 0 :(得分:4)
Java 7使这种类型的东西变得容易,具有很大的灵活性。我会使用带有glob模式的Java 7 PathMatcher(如果需要复杂性,则使用正则表达式模式)并在FileVistor中使用它。然后只需通过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和我链接的JavaDoc。
答案 1 :(得分:1)
两件事:
".*z1.*
总之类似
private static final FileFilter directoryFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
private static void printDirectories(File dir, string pattern) {
// Print this directory if it matches the pattern
if (dir.getName().matches(pattern)) {
System.out.println(dir.getAbsolutePath());
}
// Search all sub directories
for (String string : f.listFiles(directoryFilter)) {
printDirectories(subDir);
}
}
public static void main(String[] args) {
printDirectories(new File("."), ".*z1.*");
}