FileInputStream

In this chapter you will learn:

  1. How to Java FileInputStream
  2. Read a file with FileInputStream
  3. How to read a single byte from FileInputStream
  4. How to Skip n bytes

FileInputStream class

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);

Read a file with FileInputStream

FileInputStream overrides six of the methods in the abstract class InputStream. We can use those methods to read file content.

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/*j  a v  a 2s.c  o  m*/
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;
    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();
  }
}

Read a single byte

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

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
//j ava  2 s .c om
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) {
      }
    }
  }
}

Skip n bytes

The following code does a skip n bytes while reading the file using FileInputStream.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/*from ja  v  a 2  s.com*/
public class Main {
  public static void main(String[] args) {
    File file = new File("C:/data.dat");
    try {
      FileInputStream fin = new FileInputStream(file);
      int ch;
      fin.skip(10);
      while ((ch = fin.read()) != -1){
        System.out.print((char) ch);
      }
    } catch (FileNotFoundException e) {
      System.out.println("File not found" + e);
    } catch (IOException ioe) {
      System.out.println("Exception while reading the file " + ioe);
    }
  }
}

Next chapter...

What you will learn in the next chapter:

  1. How to use ObjectInputStream
  2. How to read data through ObjectOutputStream
  3. Object serialization and ObjectInputStream