所以,我基本上使用了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)
你可以張貼堆棧跟蹤和線路被拋例外? –
已更新帖子。 –