/* * Important! You must import the util and io libraries for this to work! */ import java.util.Random; import java.util.StringTokenizer; import java.io.*; /* * This example program illustrates how to use a BufferedReader to read * line-by-line from an input file, how to use a StringTokenizer to * read words from a string. It also illustrates the use of exception * handling by try-catch to handle the potential I/O errors. */ public class PrintWords { public static void main(String[] args) { String filename = args[0]; // We need try-catch because of potential problems with the files. try { /* * We create a BufferedReader from the filename which is the first * first command-line argument. */ BufferedReader inputReader = new BufferedReader(new FileReader(filename)); /* The following while loop reads from the file line-by-line. */ boolean moreInput = true; String oneLine; while (moreInput) { // Read a line from the file. oneLine = inputReader.readLine(); if (oneLine == null) { moreInput = false; } else { StringTokenizer wordGetter = new StringTokenizer(oneLine); /* * The following while loop reads individual * words from a string and inserts them into * the linked list */ while (wordGetter.hasMoreTokens()) { // Get a single word from the line String oneWord = wordGetter.nextToken(" \t\n\",.;:?()"); // Just print out the word System.out.println(oneWord); } } } /* Now we close the input file. */ inputReader.close(); } catch(IOException anError) { // Show the function call stack for debugging. anError.printStackTrace(); } } }