FileInputStream read(byte[]) in Java

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

Syntax:

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

This method takes one argument. This method reads up to b.length bytes of data from this input stream into an array of bytes.

Note: This method blocks until some input is available.

Parameters: One parameter is required for this method.

b: the buffer into which the data is 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:

IOException - if an I/O error occurs.

Approach

Java

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

public class FileInputStreamread2 {

    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' };

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

Output:

-1


No comments:

Post a Comment