FileInputStream


java.lang.Object
 |
 +-java.io.InputStream
    |
    +-java.io.FileInputStream

The FileInputStream class creates an InputStream that you can use to read bytes from a file.

Its two most common constructors:

FileInputStream(String filepath)
filepath is the full path name of a file
FileInputStream(File fileObj)
fileObj is a File object that describes the file.

Either can throw a FileNotFoundException.

The following example creates two FileInputStreams that use the same disk file and each of the two constructors:


FileInputStream f0 = new FileInputStream("/autoexec.bat") 
File f = new File("/autoexec.bat"); 
FileInputStream f1 = new FileInputStream(f);

FileInputStream overrides six of the methods in the abstract class InputStream.

int available()
Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
void close()
Closes this file input stream and releases any system resources associated with the stream.
FileChannel getChannel()
Returns the unique FileChannel object associated with this file input stream.
FileDescriptor getFD()
Returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream.
int read()
Reads a byte of data from this input stream.
int read(byte[] b)
Reads up to b.length bytes of data from this input stream into an array of bytes.
int read(byte[] b, int off, int len)
Reads up to len bytes of data from this input stream into an array of bytes.
long skip(long n)
Skips over and discards n bytes of data from the input stream.

Revised from Open JDK source code

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

public class Main {
  public static void main(String args[]) throws IOException {
    InputStream f = new FileInputStream("FileInputStreamDemo.java");
    int size = f.available();
    System.out.println("Total Available Bytes: " + size);
    int n = 40;
    System.out.println("First " + n + " bytes of the file one read() at a time");
    for (int i = 0; i < n; i++) {
      System.out.print((char) f.read());
    }
    byte b[] = new byte[n];
    if (f.read(b) != n) {
      System.err.println("couldn't read " + n + " bytes.");
    }
    System.out.println(new String(b, 0, n));

    f.skip(30);
    if (f.read(b, n / 2, n / 2) != n / 2) {
      System.err.println("couldn't read " + n / 2 + " bytes.");
    }
    System.out.println(new String(b, 0, b.length));
    f.close();
  }
}
  

The following code reads one byte from the 'a.htm'


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

public class Main {
  public static void main(String[] args) throws FileNotFoundException {
    FileInputStream file = null;
    byte x = -1;
    try {
      file = new FileInputStream("a.htm");
      x = (byte) file.read();
    } catch (FileNotFoundException f) {
      throw f;
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (file != null) {
          file.close();
        }
      } catch (IOException e) {
      }
    }
  }
}
Home 
  Java Book 
    File Stream  

FileInputStream:
  1. FileInputStream
  2. Skip n bytes while reading the file using FileInputStream
  3. FileInputStream and FileOutputStream to copy a file