Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Java by (10.2k points)

I am playing a bit with the new Java 7 IO features, actually I trying to receive all the xml files of a folder. But this throws an exception when the folder does not exist, how can I check if the folder exists with the new IO?

public UpdateHandler(String release) {

    log.info("searching for configuration files in folder " + release);

    Path releaseFolder = Paths.get(release);

    try(DirectoryStream<Path> stream = Files.newDirectoryStream(releaseFolder, "*.xml")){

        for (Path entry: stream){

            log.info("working on file " + entry.getFileName());

        }

    }

    catch (IOException e){

        log.error("error while retrieving update configuration files " + e.getMessage());

    }

}

1 Answer

0 votes
by (46k points)

You need to transform your Path into a File and test for existence:

for(Path entry: stream){

  if(entry.toFile().exists()){

    log.info("working on file " + entry.getFileName());

  }

}

Related questions

0 votes
1 answer
asked Oct 29, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Jul 9, 2019 in Java by prachi95 (1.1k points)
0 votes
2 answers
asked Jul 9, 2019 in Java by Anvi (10.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...