Searching in folder

☆樱花仙子☆ 提交于 2019-12-13 10:51:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!