我想hava一個靜態路徑屬性被掃描的兒童文件,此路徑引用一個zip文件,我不能關閉它,因爲它會被隨機引用。在某些時候,我需要寫入zip文件。問題是我不想關閉文件系統,因爲Path屬性需要打開供將來閱讀,如果我不關閉它,我看不到寫入的更改。如果我嘗試創建另一個引用相同路徑的文件系統,我會得到FileSystemAlreadyExistsException。我可以在寫完後關閉fs,然後再打開它,但有人可以嘗試在此刻讀取Path。 有一種解決方法?爲什麼我不需要關閉默認的FileSystem來查看寫入的文件,而且我確實需要使用Zip FileSystem?寫郵編文件系統與其他fs打開
public static void copyTree(final Path source, final Path targetPath) throws IOException {
FileSystem fs = FileSystems.getFileSystem(targetPath.toUri());
Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Files.copy(dir, targetPath, StandardCopyOption.REPLACE_EXISTING);
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.copy(file, targetPath, StandardCopyOption.REPLACE_EXISTING);
return super.visitFile(file, attrs);
}
});
//if I don't close the FileSystem here the changes won't appear.
fs.close();
}
'fs'是你的榜樣完全無用的,它什麼都不做,而不是正在使用的任何東西,除了'close' - 基本上,你打開一個文件並再次關閉它而不明確讀取它。 – specializt 2017-01-18 13:02:38