BufferedReader read(char[], int, int) in Java

read(char[], int, int): This method is available in the java.io.BufferedReader class of Java.

Syntax:

int java.io.BufferedReader.read(char[] cbuf, int off, int len) throws IOException

This method takes three arguments. This method reads characters into a portion of an array.

This iterated read continues until one of the following conditions becomes true:

1. The specified number of characters has been read.

2. The read method of the underlying stream returns -1, indicating the end-of-file.

3. The ready method of the underlying stream returns false, indicating that further input requests would block.

Parameters: Three parameters are required for this method.

cbuf: Destination buffer.

off: Offset at which to start storing characters.

len: Maximum number of characters to read.

Returns: The number of characters reads, or -1 if the end of the stream has been reached.

Throws:

1. IOException - If an I/O error occurs.

2. IndexOutOfBoundsException - If off is negative, or len is negative, or len is greater than cbuf.length - off

Approach 1: When no exception

Java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderread {
    public static void main(String[] args) throws IOException {

        FileReader fileReader = new FileReader("D:\\hello.txt");
        BufferedReader bufferedReader =
new BufferedReader(fileReader);

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        int off = 0, len = 3;
        System.out.println(bufferedReader.read(cbuf, off, len));
        bufferedReader.close();

    }
}

Output:

3


Approach 2: IOException

Java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderread {
    public static void main(String[] args) throws IOException {

        FileReader fileReader = new FileReader("D:\\hello.txt");
        BufferedReader bufferedReader =
new BufferedReader(fileReader);

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        int off = 0, len = 3;
        bufferedReader.close();
        System.out.println(bufferedReader.read(cbuf, off, len));

    }
}

Output:

Exception in thread "main" java.io.IOException: Stream closed at java.base/java.io.BufferedReader.ensureOpen(BufferedReader.java:122) at java.base/java.io.BufferedReader.read(BufferedReader.java:279)



Approach 3: IndexOutOfBoundsException 

Java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderread {
    public static void main(String[] args) throws IOException {

        FileReader fileReader = new FileReader("D:\\hello.txt");
        BufferedReader bufferedReader =
new BufferedReader(fileReader);

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        int off = 0, len = 10;
        System.out.println(bufferedReader.read(cbuf, off, len));
        bufferedReader.close();

    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.BufferedReader.read(BufferedReader.java:282)


No comments:

Post a Comment