Read a file input stream - Java File Path IO

Java examples for File Path IO:Binary File

Description

Read a file input stream

Demo Code

import java.io.FileInputStream;
import java.io.IOException;

public class Main {
  public static void main(String[] arguments) {
    try (FileInputStream file = new FileInputStream("save.gif")) {
      boolean eof = false;
      int count = 0;
      while (!eof) {
        int input = file.read();
        System.out.print(input + " ");
        if (input == -1)
          eof = true;//from  w w  w.ja  v  a  2 s.  c  o m
        else
          count++;
      }
      file.close();
      System.out.println("\nBytes read: " + count);
    } catch (IOException e) {
      System.out.println("Error -- " + e.toString());
    }
  }
}

Result


Related Tutorials