[英]Java, search files of given pattern and get the directory names and complete filenames
我是Java新手。 在/ var / data的所有子目录中寻找代码以搜索扩展名为.ofg的文件。
所需的输出是
有一些可用的教程,但是我找不到适合我的代码库的任何内容。 喜欢
public class FindFiles {
int inProcThreshold = 0;
protected File recurfile(File file) {
File[] dirlist = file.listFiles();
for (File f : dirlist) {
if (f.isDirectory()) {
return f;
}
}
return null;
}
protected int numOfInProcs(String location, int level, int maxdepth) {
File base = new File(location);
File[] firstlevelfiles = base.listFiles();
while (level <= maxdepth) {
for (File afile : firstlevelfiles) {
if (afile.isDirectory()) {
base = recurfile(afile);
} else {
if (afile.getName().endsWith(".txt")) {
inProcThreshold++;
}
}
}
level++;
}
return inProcThreshold;
}
public static void main(String[] args) {
FindFiles test = new FindFiles();
String dirToList = "I:\\TEST-FOLDER";
String ext = ".txt";
int count = test.numOfInProcs(dirToList, 0, 10);
System.out.println("Number of txt files are " + count);
}
}
这是我正在尝试的代码,但它向我返回0
。 我正在尝试在I:\\ TEST-FOLDER子文件夹中搜索具有extension.txt的文件。
通过在dirName参数中提供目录addres来使用此过滤器,它将列出所有扩展名为.ofg的目录
import java.io.File;
import java.io.FilenameFilter;
public class Filter {
public File[] finder( String dirName){
File dir = new File(dirName);
return dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String filename)
{ return filename.endsWith(".ofg"); }
} );
}
}
我认为您正在寻找的是Files.find 。 向其传递一个谓词,该谓词会检查path.toString()。endsWith(“。ofg”),
它将返回代表匹配文件的Path对象流。 您可以通过在此Stream上进行迭代来提取所需的所有数据。
如果不需要您自己编写递归部分(用于练习或作为任务),则可以将Files#walkFileTree与FileVisitor接口的自定义实现一起使用(@ Mena在他的评论中建议)。
扩展SimpleFileVisitor
类(或实现FileVisitor
接口),并提供要在每个文件上执行的代码:
public class OfgFolderCollectingFileVisitor extends SimpleFileVisitor<Path> {
/** Stores the matching file paths */
private final List<Path> collectedPaths = new LinkedList<>();
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
// check if the current file is an .ofg file
if (file.toString().endsWith(".ofg")) {
// it is -> add it's containing folder to the collection
this.collectedPaths.add(file.getParent());
}
return super.visitFile(file, attrs);
}
public List<Path> getCollectedPaths() {
return this.collectedPaths;
}
}
然后将实现的实例传递给Files#walkFileTree,然后检查收集的路径:
final OfgFolderCollectingFileVisitor visitor = new OfgFolderCollectingFileVisitor();
try {
Files.walkFileTree(Paths.get("/var/data"), visitor);
} catch (final IOException ex) {
ex.printStackTrace();
return;
}
// let's see if something matched our criteria
final List<Path> ofgContainers = visitor.getCollectedPaths();
System.out.printf("Files found: %d%n", ofgContainers.size());
if (!ofgContainers.isEmpty()) {
System.out.printf("%nContaining directories:%n");
for (final Path ofgContainer : ofgContainers) {
System.out.printf("- %s%n", ofgContaininer);
}
}
这是一些示例输出(是的,folder2 及其子文件夹包含一个.ofg文件)
Files found: 3
Containing directories:
- \var\data\folder1\folder1.1
- \var\data\folder2
- \var\data\folder2\folder2.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.