ClosedByInterruptException - 重新打开文件通道

时间:2014-08-05 15:08:47

标签: java nio

我正在为一个生成Thread.interrupt并使所有NIO通道无效的程序编写代码。

一旦发生这种情况,我需要恢复这些渠道中的数据,但我无法找到一种方法,因为在检查 FileChannel.isChannelOpen()的意义上所有渠道都是“无效的”返回TRUE但调用FileInputStream.getChannel()或FileOutputStream.getChannel()后会抛出 ClosedByInterruptException ,因此通道既不打开也不关闭...

File theFile = new File("myfile.txt"); //this file was already open in another function and has been "invalidated" by Thread.interrupt() call

boolean isOpen = FileChannel.isChannelOpen() //returns true
fins = new FileInputStream(theFile).getChannel() //throws ClosedByInterruptException 
fops = new FileOutputStream(theFile).getChannel() //throws ClosedByInterruptException 

明确关闭频道然后重新打开它会重现问题。

使用 Files.copy(source,target,REPLACE_EXISTING)复制文件; 也没有帮助,因为我在Java7中假设它使用下面的NIO并且它只是移动相同的通道指针这里

0 个答案:

没有答案
相关问题