所以,我基本上使用了java.io.File之前的所有东西。 但现在,当切换到java.nio.Path,我遇到一些问题...Java Path异常
我用它的基本上是加载/保存文件,在我的程序启动和关闭。
我用它在多个地方,但我会键入一个例子:
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);
}
我的问题是,每次我做一些类似的,即使使用的路径之前抛出一个异常NoSuchFileException ...但是我不知道我在做什么错,因为我在创建Path之后检查它是否存在。
更新
异常堆栈跟踪如下:
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)
你可以张贴堆栈跟踪和线路被抛例外? –
已更新帖子。 –