ShortWords.java
import java.awt.*;
import java.io.*;
import java.awt.event.*;
// A program to count the number of short words in a
// dictionary file, and write the result in another file
public class ShortWords {
public static void main(String[] args) {
BufferedReader words; // stream of dictionary words
PrintWriter result;
String curWord; // the current word
int count = 0; // count of short words seen
try {
// create a stream to access dictionary file
words = new BufferedReader(new FileReader(args[0]));
// create the output stream to write the answer
result = new PrintWriter(new FileWriter(new File(args[1])));
// get the first line of the file
curWord = words.readLine();
while ( curWord != null ) {
if ( curWord.length() == 4 ) {
count++;
result.println(curWord);
}
// get the next line from the file
curWord = words.readLine();
}
result.close();
words.close();
System.out.println("Total = " + count );
} catch ( IOException e) {
System.out.println(e);
}
}
}