简体   繁体   中英

Issue with wildcard directory search

I am running into an issue with trying to validate the existence of a file in a directory utilizing a wildcard.

The method is supposed to determine if a file with name FAACIFP_(year)(cycle).DAT could be found in the current directory.

I've tried running a PathMaker.matches("glob:FAACIFP_18") with IF/ELSE and when I tested it, it would always evaluate TRUE. So, I tried a lambda function, and it won't find the file.

Shouldn't the lambda below print the file name, or am I misunderstanding/misusing it?

static void getCnvrtdCifpName() throws IOException{
        String cnvFileName = "FAACIFP_"+Year.now()
                .format(DateTimeFormatter.ofPattern("yy"));
        Path cnvFilePath = Paths.get(System.getProperty("user.dir"));
        Files.find(cnvFilePath,0,(path,attr) -> 
                path.getFileName().startsWith(cnvFileName)).forEach(System.out::println);
        System.out.println("EVALUATING METHOD....\t SEARCH PATH: "+cnvFilePath
                +" \n\t\t\t FILE STRING: "+cnvFileName);
    }

Output:

run:
Located RAW CIFP file FAACIFP18

EVALUATING METHOD....    SEARCH PATH: C:\Users\u314170\Documents\Personal\Java\NetBeans\A424Parser 
             FILE STRING: FAACIFP_18
BUILD SUCCESSFUL (total time: 0 seconds)

Two problems: 1) change the maxdepth in your find call to 1 (from zero) 2) path.getFileName() returns a full pathname. You will want to evaluate only the filename portion (look at getName(getNameCount()-1)

After the suggestions, I finally found a resolution:

static void getCnvrtdCifpName() throws IOException{
        String cnvFileName = "FAACIFP_"+Year.now()
                .format(DateTimeFormatter.ofPattern("yy"));
        Path cnvFilePath = Paths.get(System.getProperty("user.dir"));
        DirectoryStream<Path> cnvFileStream = Files.newDirectoryStream(cnvFilePath,cnvFileName+"*");
        System.out.println("EVALUATING METHOD....\t SEARCH PATH: "+cnvFilePath
                +" \n\t\t\t FILE STRING: "+cnvFileName);
        List cnvDirList = new ArrayList();
        for(Path file: cnvFileStream){
            cnvDirList.add(file.getFileName().toString());
        }
        cnvFileStream.close();
        System.out.println(cnvDirList);
    }

With output:

run:
EVALUATING METHOD....    SEARCH PATH: C:\Users\u314170\Documents\Personal\Java\NetBeans\A424Parser 
             FILE STRING: FAACIFP_18
[FAACIFP_1808.dat, FAACIFP_1810.txt, FAACIFP_1811.txt]
BUILD SUCCESSFUL (total time: 0 seconds)

I really enjoy the problem-solving challenges while learning Java. There are so many resources out there, and many different ways to resolve an issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM