How to check a file if exists with wildcard in Java?

前端 未结 7 661
谎友^
谎友^ 2020-11-27 21:34

I have a directory, and inside it are files are named \"a_id_XXX.zip\".

How do check if a file exists given an id and File dir

相关标签:
7条回答
  • 2020-11-27 21:54

    Using Java 8 find all files in directory and sub directory with required regex condition

    My condition is to find all files with Audit_Report_xxxxxx.xls

    // get List of Audit Report file in directory and sub directory
    public List<String> getAuditReporFile(String baseDirectory) throws Exception{
    
        // select all available files in directory and subdirectory
        return Files.walk(Paths.get(baseDirectory))
                .filter(Files::isRegularFile).filter(x-> {
    
                    // use regex and check if file name is according to our requirement
                    return Pattern.compile("Audit_Report_.*\\.xls").matcher(x.getFileName().toString()).find();
    
                }).map(x->x.getFileName().toString()).collect(Collectors.toList());
    }
    
    0 讨论(0)
提交回复
热议问题