Discuss / 手写Spring / 将FileSystems.newFileSystem用try-with-resource包裹后运行测试中的scanJar()抛出ClosedFileSystemExcept异常

将FileSystems.newFileSystem用try-with-resource包裹后运行测试中的scanJar()抛出ClosedFileSystemExcept异常

Topic source

R8HLR6CC

#1 Created at ... [Delete] [Delete and Lock User]

请问廖老师为什么我将原来代码改成现在这样运行测试代码中的scanJar()方法会抛出java.nio.file.ClosedFileSystemException异常呢,运行scanClass()和scanTxt()是不会的

Path jarUriToPath(String basePackagePath, URI jarUri) throws IOException {
    return FileSystems.newFileSystem(jarUri, Map.of()).getPath(basePackagePath);
}

Path jarUriToPath(String basePackagePath, URI jarUri) throws IOException {
    try (FileSystem fileSystem = FileSystems.newFileSystem(jarUri, Map.of())) {
        return fileSystem.getPath(basePackagePath);
    }
}

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

网上搜的代码,能用就不要改

return 后,FileSystem被自动close,可能是有其他部分共用了这个FileSystem。例如:Paths中封装了活动Path的工具方法 , 其实现默认依赖于FileSystems


  • 1

Reply