//WordsFile.java //read File of strings (space seperated "words") //Usage: java Words inputFileName import java.io.*; import java.util.*; public class WordsFile { public static void main( String[] args ) { String fileName=null; BufferedReader wordFile; String line, word; StringTokenizer tokens; int totalWords=0; if (args.length > 0) //there is a command line argument fileName = args[0]; else { System.out.println("Usage: java Words inputFileName"); System.exit(1); } try { wordFile = new BufferedReader( new FileReader(fileName)); line = wordFile.readLine(); while (line != null) { tokens = new StringTokenizer(line); //space separated //tokens = new StringTokenizer(line," \t\r\n\\\"!@#$%^&*()-_=+[]{}';:|/?.>,<`~"); while (tokens.hasMoreTokens()) { word = tokens.nextToken(); totalWords++; System.out.println(word); } line = wordFile.readLine(); } wordFile.close(); System.out.println("total words: "+totalWords); } catch (IOException e) { System.out.println( "threw an exception...File does not exist?" ); } } }