使用Java查找文件夹中的文件 [英] Find files in a folder using Java
问题描述
我需要做的,如果搜索文件夹说 C:\example
What I need to do if Search a folder say 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我想然后将该文件名放入一个文件文件=新文件(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过滤器)
。
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查找文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!