如何使用 java.nio.channels.FileChannel 读取 ByteBuffer 实现类似 BufferedReader#readLine() 的行为

发布于 2024-11-15 21:35:17 字数 1428 浏览 6 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒江雪… 2024-11-22 21:35:18

原因是您需要使用 FileOutpuStream 而不是 FileInputStream。
请尝试此代码:

        FileOutputStream outStream = null;
        BufferedWriter bufWriter = null;
        FileChannel channel = null;
        FileLock lock = null;
        try{
            outStream = new FileOutputStream(file);
            channel  = outStream.getChannel();
            lock = channel.lock();
            bufWriter = new BufferedWriter(new OutputStreamWriter(outStream));
        }catch(IOException e){
            e.printStackTrace();
        }

此代码对我来说效果很好。

NUllPointerException 实际上隐藏了真正的异常,即 NotWritableChannelException。对于锁定,我认为我们需要使用 OutputStream 而不是 InputStream。

The reason is that you need to use FileOutpuStream instead of FileInputStream.
Please try this code:

        FileOutputStream outStream = null;
        BufferedWriter bufWriter = null;
        FileChannel channel = null;
        FileLock lock = null;
        try{
            outStream = new FileOutputStream(file);
            channel  = outStream.getChannel();
            lock = channel.lock();
            bufWriter = new BufferedWriter(new OutputStreamWriter(outStream));
        }catch(IOException e){
            e.printStackTrace();
        }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文