如果搜索文件夹说 C:\example
然后我需要检查每个文件并检查它是否匹配一些起始字符,以便文件开始
temp****.txt
tempONE.txt
tempTWO.txt
因此,如果文件以 temp 开头并具有扩展名 .txt 我想将该文件名放入 File file = new File("C:/example/temp***.txt);
这样我就可以读入文件,然后循环需要移动到下一个文件以检查它是否符合上述要求。
最佳答案
你想要的是File.listFiles(FileNameFilter filter)
.
这将为您提供目录中与特定过滤器匹配的文件列表。
代码将类似于:
// your directory
File f = new File("C:\\example");
File[] matchingFiles = f.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("temp") && name.endsWith("txt");
}
});
关于java - 使用 Java 在文件夹中查找文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4852531/