-2

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?

Kevin
  • 53,822
  • 15
  • 101
  • 132
edi233
  • 3,511
  • 13
  • 56
  • 97
  • where is the variable with the file name to be searched? – Aditya Naidu Nov 12 '11 at 15:37
  • I think you're trolling the people here, posting code with naming different than English. – hovanessyan Nov 12 '11 at 15:42
  • omg, this is my choice how I named my variables. – edi233 Nov 12 '11 at 15:48
  • The variable names are part of the code documentation and if you're sharing code, you should make sure your documentation is understandable by the people you're sharing code with. – Beat Nov 12 '11 at 15:56
  • Rather than putting the found files in a static member, you should probably return the `List` from the method. This will make your code easier to maintain and understand. – erickson Nov 12 '11 at 15:58
  • Ok. Next time I named my variables in English for better understanding. – edi233 Nov 12 '11 at 16:00

1 Answers1

3

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;
     }
   }
}
Mat
  • 202,337
  • 40
  • 393
  • 406
itun
  • 3,439
  • 12
  • 51
  • 75