FileInputStream read(byte[], int, int) in Java

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

Syntax:

int java.io.FileInputStream.read(byte[] b, int off, int len) throws IOException

This method takes three arguments. This method reads up to len bytes of data from this input stream into an array of bytes.

Note: If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned.

Parameters: Three parameters are required for this method.

b: the buffer into which the data is read.

off: the start offset in the destination array b.

len: the maximum number of bytes read.

Returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.

Throws:

1. NullPointerException - If b is null.

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

3. IOException - if an I/O error occurs.

Approach 1: When no exception

Java

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

public class FileInputStreamread3 {

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

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);
        FileInputStream fileInputStream =
new FileInputStream(file);

        byte b[] = { 'a', 'b', 'c', 'c' };

        int off = 0, len = 2;

        System.out.println(fileInputStream.read(b, off, len));
        fileInputStream.close();
    }
}

Output:

-1


Approach 2: NullPointerException 

Java

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

public class FileInputStreamread3 {

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

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);
        FileInputStream fileInputStream =
new FileInputStream(file);

        byte b[] = null;

        int off = 0, len = 2;

        System.out.println(fileInputStream.read(b, off, len));
        fileInputStream.close();
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.io.FileInputStream.readBytes(Native Method) at java.base/java.io.FileInputStream.read(FileInputStream.java:271)



Approach 3: IndexOutOfBoundsException 

Java

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

public class FileInputStreamread3 {

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

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);
        FileInputStream fileInputStream =
new FileInputStream(file);

        byte b[] = { 'a', 'b', 'c', 'c' };

        int off = -1, len = 2;

        System.out.println(fileInputStream.read(b, off, len));
        fileInputStream.close();
    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.FileInputStream.readBytes(Native Method) at java.base/java.io.FileInputStream.read(FileInputStream.java:271)


No comments:

Post a Comment