Windows 中的 java.io.IOException : The process cannot access the file because another process has locked a portion - when using IOUtils. copyLarge()

标签 java windows file-io

问题源于此 try block 中的特定代码行:

try {
            fInputStream = new FileInputStream(path);
#thisLine   byteCount += IOUtils.copyLarge(fInputStream, fOutputStream);
            fileCount++;
    }

堆栈跟踪看起来像这样:

java.io.IOException: The process cannot access the file because another 
process has locked a portion 
 of the file 
        at java.io.FileInputStream.readBytes(Native Method) 
        at java.io.FileInputStream.read(FileInputStream.java:233) 
        at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1719) 
        at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1696) 

这似乎是 Windows 特有的问题。是否有一些我可能遗漏的特定于 Windows 的文件 I/O 最佳实践?

最佳答案

我通过传递之前创建的 fileChannel 对象解决了这个问题。

FileChannel fileChannel = FileChannel.open(path, 
                   StandardOpenOption.READ,StandardOpenOption.WRITE);

然后我像这样创建了 InputStream 对象:

Channels.newInputStream(fileChannel);

成功了,没有再发生异常。

关于Windows 中的 java.io.IOException : The process cannot access the file because another process has locked a portion - when using IOUtils. copyLarge(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31288614/

相关文章:

java - 如何处理这个InputMismatchException?

java - 移除智能卡后 session 断开客户端

java - java中导入静态单例没有错误

Java Add() 和 Multiply() 方法返回 0/0 和 1/1?

windows - 如何编写运行 vcvars32.bat 的构建批处理脚本,然后继续构建?

c - Visual Studio 2012是否适合C(不是C++)专业编程?

c - 模拟文件系统访问

java - BufferedReader.reset() 方法出现异常

python - 在TensorFlow中,字符串常量的打印总是附有 'b'

c++ - 一次读取完整文件是个好主意吗?