问题
I want to create a method that will search files by name. I have variable with a name and I need method that search file with that name in one folder. This is my sample code:
public class Searching {
File file = new File("C:/Dane DMS/");
static ArrayList <String>listaPlikowJava = new ArrayList <String> ();
public void szukanie(File file)
{
for (File szukany : file.listFiles())
{
if(szukany.isDirectory())
szukanie(szukany);
else {
String temp[] = szukany.getName().split(".");
if (temp[1].equals("a")) listaPlikowJava.add(szukany.getName());
}
}
}
}
What do you think about this idea?
回答1:
Good one, but it is a more clear to use listFiles(FileFilter filter)
public class MyFileFilter implements FileFilter {
public boolean accept(File pathname) {
if (pathname.isDirectory())
return false;
else {
String temp[] = pathname.getName().split(".");
if (temp[1].equals("a")) return true;
}
}
}
来源:https://stackoverflow.com/questions/8105401/searching-in-folder