如何使用 java.nio.channels.FileChannel 读取 ByteBuffer 实现类似 BufferedReader#readLine() 的行为
我想使用 java.nio.channels.FileChannel 来读取文件,但我想像 BufferedReader#readLine() 那样逐行读取。我需要使用 java.nio.channels.FileChannel 而不是 java.io 的原因是因为我需要对文件加锁,并逐行读取从该锁定文件。所以我被迫使用java.nio.channels.FileChannel
。请帮助
编辑这是我的代码尝试使用FileInputStream来获取FileChannel
public static void main(String[] args){
File file = new File("C:\\dev\\harry\\data.txt");
FileInputStream inputStream = null;
BufferedReader bufferedReader = null;
FileChannel channel = null;
FileLock lock = null;
try{
inputStream = new FileInputStream(file);
channel = inputStream.getChannel();
lock = channel.lock();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
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(inputStream != null) inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
,当代码在这里lock = channel.lock();
时,它会立即转到< code>finally 且 lock
仍为 null
,因此 lock.release()
生成 NullPointerException
。我不知道为什么。
I want to use java.nio.channels.FileChannel
to read from a file, but I want to read line per line like BufferedReader#readLine()
does. The reason why I need to use java.nio.channels.FileChannel
instead of java.io
is because I need to put a lock on a file, and read line by line from that lock file. So I am force to use java.nio.channels.FileChannel
. Please help
EDIT Here is my code trying to use FileInputStream to get the FileChannel
public static void main(String[] args){
File file = new File("C:\\dev\\harry\\data.txt");
FileInputStream inputStream = null;
BufferedReader bufferedReader = null;
FileChannel channel = null;
FileLock lock = null;
try{
inputStream = new FileInputStream(file);
channel = inputStream.getChannel();
lock = channel.lock();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
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(inputStream != null) inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
when the code is here lock = channel.lock();
, it is immediately go to the finally
and lock
is still null
, so lock.release()
generate NullPointerException
. I am not sure why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是您需要使用 FileOutpuStream 而不是 FileInputStream。
请尝试此代码:
此代码对我来说效果很好。
NUllPointerException 实际上隐藏了真正的异常,即 NotWritableChannelException。对于锁定,我认为我们需要使用 OutputStream 而不是 InputStream。
The reason is that you need to use FileOutpuStream instead of FileInputStream.
Please try this code:
This code works fine for me.
The NUllPointerException is actually hiding the real exception i.e. NotWritableChannelException. For locking i think we need to use OutputStream instead of InputStream.