How to check if a folder exists?

java

Solution

Using `java.nio.file.Files`:

Path path = ...;

if (Files.exists(path)) {
    // ...
}

You can optionally pass this method `LinkOption` values:

if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {

There's also a method `notExists`:

if (Files.notExists(path)) {

Problem

I am playing a bit with the new Java 7 IO features. Actually I am trying to retrieve all the XML files in a folder. However this throws an exception when the folder does not exist. How can I check if the folder exists using 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()); } } ```

Original source

Related problems