转载

如何检查Java中是否存在带通配符的文件?

如何根据id和File目录检查文件是否存在?

FileFilter (此处编码为 anonymously )传递到dir FilelistFiles()

方法,如下所示:

File dir = new File("some/path/to/dir");
final String id = "XXX"; // needs to be final so the anonymous class can use it
File[] matchingFiles = dir.listFiles(new FileFilter() {
    public boolean accept(File pathname) {
        return pathname.getName().equals("a_id_" + id + ".zip");
    }
});

捆绑为方法,它看起来像:

public static File[] findFilesForId(File dir, final String id) {
    return dir.listFiles(new FileFilter() {
        public boolean accept(File pathname) {
            return pathname.getName().equals("a_id_" + id + ".zip");
        }
    });
}

你可以称之为:

File[] matchingFiles = findFilesForId(new File("some/path/to/dir"), "XXX");

或者只是检查是否存在,

boolean exists = findFilesForId(new File("some/path/to/dir"), "XXX").length > 0

翻译自:https://stackoverflow.com/questions/8752034/how-to-check-a-file-if-exists-with-wildcard-in-java

原文  https://codeday.me/bug/20190110/516002.html
正文到此结束
Loading...