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

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

Syntax:

int java.io.InputStreamReader.read(char[] cbuf, int offset, int length) throws IOException

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

Parameters: Three parameters are required for this method.

cbuf: Destination buffer.

offset: Offset at which to start storing characters.

length: 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

3. NullPointerException - if cbuf is null.

Approach 1: When no exception

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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

        File file = new File("D:\\hello.txt");
        InputStream in = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(in);

        char cbuf[] = { 'a', 'b', 'c', 'd', 'e' };
        int offset = 0, length = 3;

        try {
            System.out.println(inputStreamReader.read(cbuf, offset, length));
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException occurs");
        } catch (IOException e) {
            System.out.println("IOException occurs");
        }

        inputStreamReader.close();
    }
}

Output:

-1


Approach 2: IOException 

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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

        File file = new File("D:\\hello.txt");
        InputStream in = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(in);

        char cbuf[] = { 'a', 'b', 'c', 'd', 'e' };
        int offset = 0, length = 3;

        inputStreamReader.close();
        try {
            System.out.println(inputStreamReader.read(cbuf, offset, length));
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException occurs");
        } catch (IOException e) {
            System.out.println("IOException occurs");
        }

    }
}

Output:

IOException occurs



Approach 3: IndexOutOfBoundsException 

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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

        File file = new File("D:\\hello.txt");
        InputStream in = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(in);

        char cbuf[] = { 'a', 'b', 'c', 'd', 'e' };
        int offset = 0, length = 13;

        try {
            System.out.println(inputStreamReader.read(cbuf, offset, length));
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException occurs");
        } catch (IOException e) {
            System.out.println("IOException occurs");
        }

        inputStreamReader.close();
    }
}

Output:

IndexOutOfBoundsException occurs



Approach 4: NullPointerException

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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

        File file = new File("D:\\hello.txt");
        InputStream in = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(in);

        char cbuf[] = null;
        int offset = 0, length = 3;

        try {
            System.out.println(inputStreamReader.read(cbuf, offset, length));
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException occurs");
        } catch (IOException e) {
            System.out.println("IOException occurs");
        }

        inputStreamReader.close();
    }
}

Output:

NullPointerException occurs


Some more methods of InputStreamReader

close(): This method closes the stream and releases any system resources associated with it.

InputStreamReader(InputStream): This method creates an InputStreamReader that uses the default charset.

InputStreamReader(InputStream, Charset): This method creates an InputStreamReader that uses the given charset.

InputStreamReader(InputStream, CharsetDecoder): This method creates an InputStreamReader that uses the given charset decoder.

InputStreamReader(InputStream, String): This method creates an InputStreamReader that uses the named charset.

getEncoding(): This method returns the name of the character encoding being used by this stream.

read(): This method reads a single character.

ready(): This method tells whether this stream is ready to be read. 


No comments:

Post a Comment