【发布时间】:2017-11-01 14:46:50
【问题描述】:
我打开了一个文件,然后我锁定了一个文件区域,我希望更新一个值,然后将其重写回文件。尝试写入已锁定的文件时收到以下错误消息:
java.io.IOException: The process cannot access the file because another
process has locked a portion of the file
代码示例:
// Now lets get a lock on the first 10 characters
try( RandomAccessFile raFile = new RandomAccessFile( cbooking, "rw" );
FileChannel channel = raFile.getChannel();
FileLock lock = channel.lock( 1 , 10, true) ) {
// Write the new value to the file
byte[] bytes = " ".getBytes();
raFile.write( bytes );
}
catch ( IOException e) {
e.printStackTrace();
// TODO Add logging - Could not gain character lock on the file!
return false;
}
任何意见表示赞赏。
【问题讨论】:
-
你找到答案了吗?我在 Windows 上遇到了同样的事情,这有点让人生气
标签: java ioexception