How to get ftl files from directory and subdirectry
I want to get complete list of ftl file from directory and their subdirectory with their relative path. For example suppose if I have a ftl file in D:\abc\pqr\lmn\try.ftl
and I pass D:\abc
as search path then I should get /pqr/lmn/try.ftl
and all *.ftl
files list which are present in search directory and their subdirectory with relative path as result.
Can someone 开发者_JS百科help me achieving this functionality.
- Pass dir to your method
- List out all the direct sub dir of the passed dir and look for the file with fileName using
FileNameFilter
- recursively call this for all the sub dir
Try this code...
String dir = TEMPLATE_PATH;
File folder = new File(dir);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
if (listOfFiles[i].getName().endsWith(".ftl") || listOfFiles[i].getName().endsWith(".FTL"))
{
System.out.println("[FILE]" + listOfFiles[i].getName());
fileName.add(listOfFiles[i].getName());
}
} else if (listOfFiles[i].isDirectory()) {
String directoryToSearch = listOfFiles[i].getName();
System.out.println("[DIR]" + directoryToSearch);
File subFolder = new File(dir + directoryToSearch);
File[] listOfSubFiles = subFolder.listFiles();
for (int j = 0; j < listOfSubFiles.length; j++){
if (listOfSubFiles[j].getName().endsWith(".ftl") || listOfSubFiles[j].getName().endsWith(".FTL"))
{
System.out.println("\t[DIR]" + listOfSubFiles[j].getName());
fileName.add(listOfSubFiles[j].getName());
}
}
}
}
精彩评论