Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java.nio.channels.FileChannel读取ByteBuffer实现类似BufferedReader#readLine()的行为_Java_Nio_Bytebuffer_Filechannel - Fatal编程技术网

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

如何使用java.nio.channels.FileChannel读取ByteBuffer实现类似BufferedReader#readLine()的行为,java,nio,bytebuffer,filechannel,Java,Nio,Bytebuffer,Filechannel,我想使用java.nio.channels.FileChannel读取文件,但我想像BufferedReader#readLine()那样逐行读取。我需要使用java.nio.channels.FileChannel而不是java.io的原因是,我需要在文件上设置一个锁,并逐行读取该锁文件。所以我不得不使用java.nio.channels.FileChannel。请帮忙 编辑以下是我尝试使用FileInputStream获取FileChannel的代码 public static void m

我想使用
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()
,它会立即转到
最后
,并且
锁定
仍然是
空的
,因此
锁定.释放()
生成
空点异常
。我不知道为什么。

原因是您需要使用FileOutputStream而不是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实际上隐藏了真正的异常,即。对于锁定,我认为我们需要使用OutputStream而不是InputStream。

我尝试这样做,但由于某些原因,当我尝试使用
FileInputStream
锁定文件时,效果不佳。不知道为什么,我记得我以前用过这个,没有任何问题。你能告诉我是什么吗working@Suraj:我更新了我的帖子,我的代码使用了
FileInputStream
,你能看一下吗?原因是你需要使用OutputStream而不是INputStream,我会更新答案,但我尝试从文件中读取,而不是写入。我不能使用OutputStream阅读,或者我在这里遗漏了什么?