java: search file according to its name in directory and subdirectories

前端 未结 5 999
庸人自扰
庸人自扰 2021-02-03 13:50

I need a to find file according to its name in directory tree. And then show a path to this file. I found something like this, but it search according extension. Could anybody h

5条回答
  •  鱼传尺愫
    2021-02-03 14:05

    You can use FileFilter Like this.

    public class MyFileNameFilter implements FilenameFilter {
    
    @Override
    public boolean accept(File arg0, String arg1) {
        // TODO Auto-generated method stub
        boolean result =false;
        if(arg1.startsWith("KB24"))
            result = true;
        return result;
    }
    

    }

    And call it like this

    File f = new File("C:\\WINDOWS");
        String []  files  = null;
        if(f.isDirectory()) {  
    
            files = f.list(new MyFileNameFilter());
        }
    
        for(String s: files) {
    
            System.out.print(s);
            System.out.print("\t");
        }
    

提交回复
热议问题