Program to Read text from file using FileInputStream in java file IO


FileInputStream  >
  • FileInputStream is not buffered.
  • FileInputStream reads bytes from a file.
  • Every time FileInputStream.read() is called a call is made to read a system file. FileInputStream.read() reads 1 byte (8-bit) at a time.




We will read c:/myFile.txt

c:/myFile.txt looks like this >
You are learning File IO from javaMadeSoEasy.com



Program to Read text from file using FileInputStream in java file IO >

import java.io.FileInputStream;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class ReadFileUsingFileInputStream {
   public static void main(String... args) {
          FileInputStream fis = null;
          try {
                 // if file doesn't exist FileNotFoundException will be thrown at
                 // runtime
                 fis = new FileInputStream("c:/myFile.txt");
                 System.out.println("Reading from text file using FileInputStream > ");
                 int ch;
                 while ((ch = fis.read()) != -1) { //read till end of file
                       System.out.print((char) ch);
                 }
          } catch (IOException e) {
                 e.printStackTrace();
          } finally {
                 try {
                       if (fis != null)
                              fis.close(); //close FileInputStream
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/* OUTPUT
Reading from text file using FileInputStream>
You are learning File IO from javaMadeSoEasy.com   
*/




RELATED LINKS>

Read text from file using BufferedInputStream (and FileInputStream) in java file IO


Program to Read text from file in String using BufferedInputStream (and FileInputStream) in java file IO



Program to Read text from file using FileInputStream and Try with resource provided in java 7


eEdit
Must read for you :