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

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

Syntax:

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

This method takes three arguments. This method read characters into a portion of an array. Whenever a line terminator is read the current line number is incremented.

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 bytes read, or -1 if the end of the stream has already 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.

3. NullPointerException - if cbuf is null.

Approach 1: When no exception

Java

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.StringReader;

public class LineNumberReaderread2 {
    public static void main(String[] args) {

        Reader in = new StringReader("Hello");
        LineNumberReader lineNumberReader = new LineNumberReader(in);

        char cbuf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
        int off = 0, len = 3;

        try {
            System.out.println(lineNumberReader.read(cbuf, off, len));
        } catch (IOException e) {
            System.out.println("IOException occurs");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException occurs");
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        }

    }
}

Output:

3


Approach 2: IndexOutOfBoundsException 

Java

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.StringReader;

public class LineNumberReaderread2 {
    public static void main(String[] args) {

        Reader in = new StringReader("Hello");
        LineNumberReader lineNumberReader = new LineNumberReader(in);

        char cbuf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
        int off = 0, len = 13;

        try {
            System.out.println(lineNumberReader.read(cbuf, off, len));
        } catch (IOException e) {
            System.out.println("IOException occurs");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException occurs");
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        }

    }
}

Output:

IndexOutOfBoundsException occurs


Approach 3: NullPointerException 

Java

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.StringReader;

public class LineNumberReaderread2 {
    public static void main(String[] args) {

        Reader in = new StringReader("Hello");
        LineNumberReader lineNumberReader = new LineNumberReader(in);

        char cbuf[] = null;
        int off = 0, len = 3;

        try {
            System.out.println(lineNumberReader.read(cbuf, off, len));
        } catch (IOException e) {
            System.out.println("IOException occurs");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException occurs");
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        }

    }
}

Output:

NullPointerException occurs


Some other methods of LineNumberReader

getLineNumber()This method gets the current line number.

LineNumberReader(Reader)This method creates a new line-numbering reader, using the default input buffer size.

LineNumberReader(Reader, int)This method creates a new line-numbering reader, reading characters into a buffer of the given size.

mark(int)This method marks the present position in the stream.

read()This method read a single character.

readLine()This method read a line of text.

reset()This method resets the stream to the most recent mark.

setLineNumber(int)This method sets the current line number.

skip(long)This method skips characters.

No comments:

Post a Comment