开发者

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

开发者 https://www.devze.com 2023-03-11 19:29 出处:网络
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 anyb开发者_JAVA百科ody help me h

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 anyb开发者_JAVA百科ody help me how can I rework this code to my needs...thanks

public class filesFinder {
public static void main(String[] args) {
    File root = new File("c:\\test");

    try {
        String[] extensions = {"txt"};
        boolean recursive = true;


        Collection files = FileUtils.listFiles(root, extensions, recursive);

        for (Iterator iterator = files.iterator(); iterator.hasNext();) {
            File file = (File) iterator.next();
            System.out.println(file.getAbsolutePath());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}


public class Test {
    public static void main(String[] args) {
        File root = new File("c:\\test");
        String fileName = "a.txt";
        try {
            boolean recursive = true;

            Collection files = FileUtils.listFiles(root, null, recursive);

            for (Iterator iterator = files.iterator(); iterator.hasNext();) {
                File file = (File) iterator.next();
                if (file.getName().equals(fileName))
                    System.out.println(file.getAbsolutePath());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Recursive directory search in Java is pretty darn easy. The java.io.File class has a listFiles() method that gives all the File children of a directory; there's also an isDirectory() method you call on a File to determine whether you should recursively search through a particular child.


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");
    }

Java 8 Lamda make this easier instead of using FileNameFilter, pass lambda expression

   File[] filteredFiles =  f.listFiles((file, name) ->name.endsWith(extn));
   


I don't really know what FileUtils does, but how about changing "txt" in extenstions to "yourfile.whatever"?


public static File find(String path, String fName) {
    File f = new File(path);
    if (fName.equalsIgnoreCase(f.getName())) return f;
    if (f.isDirectory()) {
        for (String aChild : f.list()) {
            File ff = find(path + File.separator + aChild, fName);
            if (ff != null) return ff;
        }
    }
    return null;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消