Java FileInputStream Class

Category: Java   Tags: Java, Java File Handling, Java InputStream Class, Java FileInputStream Class

The FileInputStream class creates an InputStream and used to read bytes from a file. It has below declaration:

                                    public class FileInputStream
                                    extends InputStream
                                

Constructors

public FileInputStream(File file) throws FileNotFoundException
Creates a FileInputStream by opening a connection to an actual file. Throws FileNotFoundException if the file does not exist.
public FileInputStream(String filePath) throws FileNotFoundException
Creates a FileInputStream by opening a connection to an actual file. The file named by the path filePath in the file system. Throws FileNotFoundException if the file does not exist.
public FileInputStream(FileDescriptor fd) throws FileNotFoundException
Creates a FileInputStream by using the file descriptor fd, which represents an existing connection to an actual file in the file system.

Methods

public int available() throws IOException
It will return the number of bytes available that can be read or skipped over from this input stream without blocking. Returns 0 when it reaches the end of the input stream.
public void close() throws IOException
Closes this file input stream and releases any system resources associated with the stream. Further read attempts will generate an IOException.
public int read() throws IOException
Reads a byte of data from this input stream. Returns -1 if the end of the file is reached.
public int read(byte[] b) throws IOException
It tries to read bytes (up to b.length bytes) into b and returns the actual number of bytes that were successfully read. Returns -1 if the end of the file is reached.
public int read(byte[] b, int offset, int num) throws IOException
It tries to read bytes (up to num bytes) into b starting at b[offset] and returns the actual number of bytes that were successfully read. Returns -1 if the end of the file is reached.
public long skip(long n) throws IOException
It skips n bytes of input, returning the number of bytes actually ignored.

Here is a simple Java program for FileInputStream:

FileInputStreamExample.java

                            package com.tutorial.java.files;

                            import java.io.FileInputStream;
                            import java.io.FileNotFoundException;
                            import java.io.IOException;

                            public class FileInputStreamExample {

                                public static void main(String[] args) throws FileNotFoundException, IOException {
                                    FileInputStream fis = new FileInputStream("sample.txt");
                                    int size = fis.available();
                                    System.out.println("Available Size: " + size);
                                    int n = size / 20;
                                    System.out.println("First " + n + " bytes of the file.");
                                    for (int i = 0; i < n; i++) {
                                        System.out.print((char) fis.read());
                                    }
                                    System.out.println("\nAvailable Size: " + fis.available());

                                    System.out.println("Reading the next " + n + " with read(b[])");
                                    byte b[] = new byte[n];
                                    if (fis.read(b) != n) {
                                        System.err.println("couldn’t read " + n + " bytes.");
                                    }
                                    System.out.println(new String(b, 0, n));
                                    size = fis.available();
                                    System.out.println("\nStill Available: " + size);
                                    System.out.println("Skipping half of remaining bytes with skip()");
                                    fis.skip(size / 2);
                                    System.out.println("Still Available: " + fis.available());

                                    fis.close();
                                }

                            }
                        

Output:

                            Available Size: 649
                            First 32 bytes of the file.
                            public FileInputStream(File file
                            Available Size: 617
                            Reading the next 32 with read(b[])
                            ) throws FileNotFoundException
                            C

                            Still Available: 585
                            Skipping half of remaining bytes with skip()
                            Still Available: 293