1

So, I used java.io.File for basically everything before. But now, when switching to java.nio.Path, I'm experiencing a few problems...

What I use it for is basically loading/saving files, on my program startup and shutdown.

I use it in multiple places but I'll type an example:

  Objects.requireNonNull(directory, "directory");

    if (this.myObjectMap.isEmpty()) {
        return;
    }

    Files.list(directory).forEach(file -> {
        try {
            Files.deleteIfExists(file);
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    });

    Files.createDirectories(directory);

    for (Object object : this.myObjectMap.values()) {
        Path destination = directory.resolve(object.toString() + ".json");

        Files.deleteIfExists(destination);
        Files.createFile(destination);

        JsonObject properties = new JsonObject();

        JSONFileHandler.save(destination, properties);
    }

My problem is that everytime I do something similar to this, it throws a NoSuchFileException exception before even using the Path... But I don't know what I'm doing wrong, since I check if it exists after creating the Path.

Update

The exception stacktrace is the following:

java.nio.file.NoSuchFileException: **the directory**

at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsDirectoryStream.<init>(WindowsDirectoryStream.java:86)
at sun.nio.fs.WindowsFileSystemProvider.newDirectoryStream(WindowsFileSystemProvider.java:518)
at java.nio.file.Files.newDirectoryStream(Files.java:457)
at java.nio.file.Files.list(Files.java:3451)
2
  • Could you post the stacktrace and line that is throwing the exception? Commented Apr 18, 2017 at 16:00
  • Updated the post. Commented Apr 18, 2017 at 16:04

1 Answer 1

2

Here's the javadoc for Path, this is what it says:

An object that may be used to locate a file in a file system. It will typically represent a system dependent file path.

So, a Path just represents a Path, it's not a pointer to an existing file or directory and hence, it may or may not exist.

In our example, we need to check whether the Path exists, before calling Files.list and that would make sure we are iterating through valid path, e.g.:

Path directory = Paths.get("some directory");
Objects.requireNonNull(directory, "directory");
if(Files.exists(directory)){
    Files.list(directory).forEach(file -> {
        try {
            System.out.println(file);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.