简体   繁体   English

FileOutputStream try-with-resources不会关闭文件描述符

[英]FileOutputStream try-with-resources doesn't close file descriptor

I have code that extracts some specific large (about 15k entries) binary serialized file archive to folder on disk. 我有代码将一些特定的大型(约15k条目)二进制序列化文件存档提取到磁盘上的文件夹。

public void extractExact(Path absolutePath, DoubleConsumer progressConsumer) throws IOException
{
    ...
    // Extract to file channel
    try (final FileOutputStream fos = new FileOutputStream(absolutePath.toFile()))
    {
        PakExtractor.Extract(pakFile, Entry, fos.getChannel(), progressConsumer);
    }
 }

extractExact function calls for every entry in archive. extractExact函数调用存档中的每个条目。

after this, if I try to call Files.delete(<archive_file_path>) method - I will get an exception: 在此之后,如果我尝试调用Files.delete(<archive_file_path>)方法 - 我将得到一个异常:

java.nio.file.FileSystemException: The process cannot access the file because it is being used by another process. java.nio.file.FileSystemException:进程无法访问该文件,因为它正由另一个进程使用。

I checked my archive file in Process Explorer search and it says that I have ~15k file openings by my java.exe (as many as the files in the archive) 我在Process Explorer搜索中检查了我的存档文件,它说我的java.exe有大约15k文件打开(与存档中的文件一样多)

this happens only in windows (jdk1.8.0_162). 这只发生在windows(jdk1.8.0_162)中。 On Linux I don't have any problems with "zombie" opened files. 在Linux上我对“僵尸”打开的文件没有任何问题。

Finally - we found the solution. 最后 - 我们找到了解决方案。 Many thanks to @Netherwire. 非常感谢@Netherwire。 FileChannel class have map method that does some implicit copy operations with file descriptors, so be careful when use it. FileChannel类具有map方法,该方法使用文件描述符执行一些隐式复制操作,因此在使用它时要小心。 Here is more information. 是更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM