// FileInputDemo.java // Reads text from a file, and prints each word on a separate line // cmc, 11/17/05 import java.io.FileReader; import java.io.IOException; import java.util.Scanner; public class FileInputDemo { public static void main (String args[]) { Scanner input = null; // try to open the file try { input = new Scanner( new FileReader("example1.dat") ); } catch (IOException e) { System.err.println("could not open file - " + e); System.exit(1); } // got a Scanner now (it handles the IOExceptions from now on ;-) // loop through the tokens in the file and print one per line while ( input.hasNext() ) System.out.println( input.next() ); } }