by Dinesh Thakur

We use FilelnputStream class for reading from the file as it creates an input byte stream for reading from the file. To read the contents from the file ecomputernotes.bat, we first make an object by name say fp of FileOutputStream class and specify the file name ecomputernotes.bat as an argument to the constructor:

fp=new FilelnputStream(ecomputernotes.bat ");

If the file is not found or some error occurs in the disk, then the exception FileNotFoundException is thrown. If the file is found and everything is well, the matter is read from the file and is displayed on the screen after casting it into characters (because data in file is in byte form). When file is over, it is closed by fp.close() method.

import java.io.*;

class FileInputStreamJavaExample
{
      public static void main(String args[]) throws IOException
      {
            int i;
            FileInputStream fp;
            try
             {
                 fp=new FileInputStream("ecomputernotes.bat");
             }
                  catch(FileNotFoundException e)
                  {
                      System.out.println("File Cannot be Found");
                      return;
                  }
                     System.out.println("Data in File is");
                    do
                      {
                          i=fp.read();
                          if(i !=-1)
                          System.out.print((char)i);
                      }while(i !=-1);
                         fp.close();
       }
 }   

FileInputStream