无法读取新锁定的文件
所以我尝试锁定文件来读取它,但我得到了 IOException,知道为什么吗?
public static void main(String[] args){
File file = new File("C:\\dev\\harry\\data.txt");
FileReader fileReader = null;
BufferedReader bufferedReader = null;
FileChannel channel = null;
FileLock lock = null;
try{
channel = new RandomAccessFile(file, "rw").getChannel();
lock = channel.lock();
fileReader = new FileReader(file);
bufferedReader = new BufferedReader(fileReader);
String data;
while((data = bufferedReader.readLine()) != null){
System.out.println(data);
}
}catch(IOException e){
e.printStackTrace();
}finally{
try {
lock.release();
channel.close();
if(bufferedReader != null) bufferedReader.close();
if(fileReader != null) fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我收到此错误IOException:该进程无法访问该文件,因为另一个进程已锁定文件的一部分
So I try to locked the file to read it, but I got IOException, any idea why?
public static void main(String[] args){
File file = new File("C:\\dev\\harry\\data.txt");
FileReader fileReader = null;
BufferedReader bufferedReader = null;
FileChannel channel = null;
FileLock lock = null;
try{
channel = new RandomAccessFile(file, "rw").getChannel();
lock = channel.lock();
fileReader = new FileReader(file);
bufferedReader = new BufferedReader(fileReader);
String data;
while((data = bufferedReader.readLine()) != null){
System.out.println(data);
}
}catch(IOException e){
e.printStackTrace();
}finally{
try {
lock.release();
channel.close();
if(bufferedReader != null) bufferedReader.close();
if(fileReader != null) fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
and I got this error IOException: The process cannot access the file because another process has locked a portion of the file
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不妨将其添加为答案而不是评论。
如果您使用
FileLock
API,则需要使用相应的NIO文件api。Might as well add this as an answer instead of a comment.
If you use the
FileLock
API you need to use the corresponding NIO file apis.复制我的答案 此处(以防被删除),并添加 Jeff Foster 的反馈:
考虑到抛出了
OverlappingFileLockException
异常的实例,它似乎同一进程中的另一个线程正在尝试锁定同一文件。这不是 A 和 B 之间的冲突,而是 B 内部的冲突,如果查看有关 lock() 方法的 API 文档以及抛出 OverlappingFileLockException 的条件:唯一解决方案是阻止 B 中的任何其他线程获取同一文件或文件中同一重叠区域的锁。
抛出的 IOException 有一些更有趣的消息。它可能证实了上述理论,但如果不查看整个源代码,我无法确认任何事情。
lock
方法预计会阻塞,直到获取独占锁。如果获取到了,那么读取文件应该没有问题。除了一个条件。如果文件已由同一 JVM 在不同线程中使用 File 对象(或者换句话说,第二个/不同的文件描述符)打开(并锁定),则对第一个文件描述符的尝试读取将失败,甚至如果获取了锁(毕竟,该锁不会锁定其他线程)。<罢工>
一种改进的设计是,每个进程中都有一个线程,仅在一定时间内获取文件上的独占锁(当使用单个文件对象或单个文件描述符时),在该文件中执行所需的活动。文件,然后释放锁。
正如 Jeff 所指出的,使用 NIO API 可能会解决问题。这完全是由于 FileReader API 可能打开一个新的文件描述符,该文件描述符与获取锁的文件描述符不同。
Reproducing my answer from here (in case it gets deleted), and adding Jeff Foster's feedback:
Considering that an instance of the
OverlappingFileLockException
exception is thrown, it appears that another thread in the same process is attempting to lock on the same file. This is not a conflict between A and B, but rather a conflict within B, if one goes by the API documentation on the lock() method and when the condition under which it throws OverlappingFileLockException:The only solution to prevent this, is to have any other thread in B prevented from acquiring a lock on the same file, or the same overlapping region in the file.
The
IOException
being thrown has a bit more interesting message. It probably confirms the above theory, but without looking at the entire source code, I cannot confirm anything. Thelock
method is expected to block until the exclusive lock is acquired. If it was acquired, then there ought to be no problem in reading from the file. Except for one condition. If the file has already been opened (and locked) by the same JVM in a different thread, using a File object (or in other words, a second/different file descriptor), then the attempted read on the first file descriptor will fail even if the lock was acquired (after all, the lock does not lock out other threads).An improved design, would be to have a single thread in each process that acquires an exclusive lock on the file (while using a single File object, or a single file descriptor) for only a certain amount of time, perform the required activity in the file, and then release the lock.
As Jeff has pointed out, using the NIO APIs would probably result in resolution of the problem. This is entirely due to the possibility of the FileReader API opening a new file descriptor, which is different from the one that the lock is obtained on.
也许你想要的更像是:
Maybe what you want is something more like: