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


public class Main {
  public static void main(String args[]) throws IOException {
    InputStream f = new FileInputStream("");
    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++) {
    byte b[] = new byte[n];
    if ( != n) {
      System.err.println("couldn't read " + n + " bytes.");
    System.out.println(new String(b, 0, n));

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

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


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);
    } catch (FileNotFoundException f) {
      throw f;
    } catch (IOException e) {
    } finally {
      try {
        if (file != null) {
      } catch (IOException e) {
  Java Book 
    File Stream  

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