FileInputStream

A FileInputStream obtains input bytes from a file which are available on local or server. FileInputStream is meant for reading streams of raw bytes such as image data, audio, video etc.

Class declaration:

public class FileInputStream extends InputStream

Constructors:

1. public java.io.FileInputStream(java.io.File) throws java.io.FileNotFoundException

Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.

2. public java.io.FileInputStream(java.io.FileDescriptor)

Creates a FileInputStream by using the file descriptor fileDescriptorObject, which represents an existing connection to an actual file in the file system.

3. public java.io.FileInputStream(java.lang.String) throws java.io.FileNotFoundException

Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.

Methods:

Below are few methods declared in FileInputStream class. They are as follows:

Method Role
public int available() Return the estimated number of bytes that can be read from the input stream.
public int read() Read the byte of data from the input stream.
int read(byte[] b) Read up to b.length bytes of data from the input stream.
int read(byte[] b, int off, int length) Read up to length bytes of data from the input stream.
public long skip(long n) Skip n bytes of data from the input stream.
public FileDescriptor getFD() Used to return the FileDescriptor object.
public void close() Used to close the stream objects.

Let us see an example:

package javaiotest;

import java.io.FileDescriptor;
import java.io.FileInputStream;

public class FileInputStreamTest {
  public static void main(String[] args) throws Exception {

    FileInputStream fis = new FileInputStream(System.getProperty("user.dir")+"\\testFile.txt");
    int i;	
    //read() method
    System.out.println("Example of read() method");
    //Read the whole content of the file
    while((i=fis.read())!=-1) {
      System.out.print((char)i);
    }

    //skip(long n) method
    //Again assign the value in fis object
    fis = new FileInputStream(System.getProperty("user.dir")+"\\testFile.txt");
    System.out.println("\n\nExample of skip(long n) method");
    //skip 2 bytes from the content
    fis.skip(2);
    while((i=fis.read())!=-1) {
      System.out.print((char)i);
    }

    //getFD() method
    System.out.println("\n\nExample of getFD()method");
    FileDescriptor fileDescriptor = fis.getFD();

    //check wheather file is valid or not
    boolean validFile = fileDescriptor.valid();
    System.out.println("Check the valid file: "+validFile);

    //read(byte[] b, int off, int len) method
    System.out.println("\n\nExample of read(byte[] b, int off, int len) method");
    //Again assign the value in fis object
    fis = new FileInputStream(System.getProperty("user.dir")+"\\testFile.txt");
    byte[] bs = new byte[5];
    // read bytes to the buffer
    int i1 = fis.read(bs, 1, 4);
    System.out.println("Number of bytes read here: " + i1);
    // for each byte in buffer
    for (byte b : bs) {
      // converts byte to character
      char c = (char) b;
      if (b == 0){
        c = '-';
      }
      System.out.print(c);
    }
  }
}

Output:

Example of read() method
Welcome in Huge Learning Website

Example of skip(long n) method
lcome in Huge Learning Website

Example of getFD()method
Check the valid file: true


Example of read(byte[] b, int off, int len) method
Number of bytes read here: 4
-Welc