gpt4 book ai didi

java - 搜索扩展名为 .java 的文件

转载 作者:行者123 更新时间:2023-11-29 04:02:57 25 4
gpt4 key购买 nike

有什么方法可以首先过滤所有扩展名为“java”的文件,然后搜索找到一些具有该扩展名的文件?你能用一段代码解释一下吗?谢谢

最佳答案

我也投票支持 Apache Commons。

http://www.kodejava.org/examples/359.html给出一个用法示例:

package org.kodejava.example.commons.io;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.Collection;
import java.util.Iterator;

public class SearchFileRecursive {
public static void main(String[] args) {
File root = new File("/home/foobar/Personal/Examples");

try {
String[] extensions = {"xml", "java", "dat"};
boolean recursive = true;

//
// Finds files within a root directory and optionally its
// subdirectories which match an array of extensions. When the
// extensions is null all files will be returned.
//
// This method will returns matched file as java.io.File
//
Collection files = FileUtils.listFiles(root, extensions, recursive);

for (Iterator iterator = files.iterator(); iterator.hasNext();) {
File file = (File) iterator.next();
System.out.println("File = " + file.getAbsolutePath());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

关于java - 搜索扩展名为 .java 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2214254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com