问题源于此 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/