// This program is a simple demonstration of how to use the Scanner class to read // input from a file. // History: // // April 2013 -- Created by Doug Baldwin and CSci 242 as a self-tutorial. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class ScannerDemo { // The program simply creates a scanner connected to a text file and // uses that scanner to read and print words one by one from the file. public static void main(String[] args) { try { File newFile = new File("words.txt"); Scanner console = new Scanner(newFile); while(console.hasNext()) { String word = console.next(); System.out.println(word); } } catch(FileNotFoundException e) { System.out.println(e.getMessage()); } } }