无法读取新锁定的文件 [英] Unable to read from newly locked file
本文介绍了无法读取新锁定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我试图锁定文件以读取它,但是我得到了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();
}
}
}
我收到此错误IOException: The process cannot access the file because another process has locked a portion of the file
推荐答案
还可以将其添加为答案而不是评论.
Might as well add this as an answer instead of a comment.
如果使用FileLock
API,则需要使用相应的NIO文件api.
If you use the FileLock
API you need to use the corresponding NIO file apis.
这篇关于无法读取新锁定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文