home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch10 / ReadStream.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  883 b   |  35 lines

  1.  
  2. import java.io.*;
  3.  
  4. public class ReadStream
  5. {
  6.     // This program runs as an application
  7.     public static void main(String[] arguments)
  8.     {
  9.         try
  10.         {
  11.             //declare a stream
  12.             FileInputStream ifile = new FileInputStream("bytes.dat");
  13. boolean eof = false;
  14.             int cntBytes = 0;
  15.             while (!eof)
  16.             {
  17.                int thisVal = ifile.read();
  18.                System.out.print( thisVal + " ");
  19.                if (thisVal == -1)
  20.                   eof = true;
  21.                else
  22.                   cntBytes++;
  23.             } //while
  24.             ifile.close();
  25.             System.out.print("\nBytes read: " + cntBytes);
  26.  
  27.        } catch (IOException e)// handle any errors
  28.             {
  29.                 System.out.println("Error - " + e.toString());
  30.             }//catch
  31.     }//main
  32. }//ReadStream
  33.  
  34.  
  35.