// Marty Stepp, CSE 143, Winter 2009 // This simple program demonstrates using Java's input and output streams to // print the contents of a file, one byte at a time. // This program also demonstrates try/catch statements for handling exceptions. import java.io.*; public class FileEcho { public static void main(String[] args) { String filename = "FileEcho.java"; try { // open a stream to read the file FileInputStream in = new FileInputStream(filename); // read each byte from the file, one at a time while (true) { int n = in.read(); if (n == -1) { // -1 means end-of-file break; } char ch = (char) n; // each byte is really a character (ASCII) System.out.print(ch); } in.close(); } catch (FileNotFoundException e) { // run this code if an exception occurs System.out.println("File not found: " + filename); } catch (IOException e) { System.out.println("I/O error occurred"); } } }