Java I/O How to - Create Stream from File object








Question

We would like to know how to create Stream from File object.

Answer

  //  w  w  w.  jav  a  2  s. co  m
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class MainClass {
  public static void main(String[] a) {
    File aFile = new File("C:/myFile.txt");
    FileInputStream inputFile = null;
    try {
      inputFile = new FileInputStream(aFile);
    } catch (FileNotFoundException e) {
      e.printStackTrace(System.err);
      System.exit(1);
    }
  }
}

The code above generates the following result.