问题描述
如果搜索文件夹说C:example
然后我需要遍历每个文件并检查它是否与几个开始字符匹配,因此文件是否开始
I then need to go through each file and check to see if it matches a few start characters so if files start
temp****.txt
tempONE.txt
tempTWO.txt
因此,如果文件以 temp 开头且扩展名为 .txt,我想将该文件名放入 File file = new File("C:/example/temp***.txt);
所以我可以读入文件,然后循环需要移动到下一个文件来检查它是否满足上述要求.
So if the file starts with temp and has an extension .txt I would like to then put that file name into a File file = new File("C:/example/temp***.txt);
so I can then read in the file, the loop then needs to move onto the next file to check to see if it meets as above.
推荐答案
你想要的是 File.listFiles(FileNameFilter filter)
.
What you want is File.listFiles(FileNameFilter filter)
.
这会给你一个目录中与特定过滤器匹配的文件列表.
That will give you a list of the files in the directory you want that match a certain 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 在文件夹中查找文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!