import java.util.Random; import java.util.StringTokenizer; import java.io.*; public class DirtyWords { public static void main(String[] args) { Random r = new Random(); try // We need try-catch because of potential problems with the files. { /* * We create a BufferedReader from the filename which is the first * first command-line argument. */ BufferedReader inputReader = new BufferedReader(new FileReader(args[0])); /* * We create a PrintWriter from the filename which is the second * command-line argument. */ PrintWriter outputWriter = new PrintWriter(new FileWriter(args[1])); /* The following while loop reads from the file line-by-line. */ boolean moreInput = true; String oneLine; while (moreInput) { oneLine = inputReader.readLine(); // Read a line from the file. if (oneLine == null) { moreInput = false; } else { StringTokenizer wordGetter = new StringTokenizer(oneLine); /* * The following while loop reads individual words from a string * and copies them word-by-word to the output file. */ while (wordGetter.hasMoreTokens()) { String aWord = wordGetter.nextToken(); if (aWord.length() == 4) { aWord = randomString(r, 4); } outputWriter.print(aWord); outputWriter.print(" "); } outputWriter.println(); } } /* Now we close the input and output files. */ inputReader.close(); outputWriter.close(); } catch(IOException anError) { anError.printStackTrace(); // Show the function call stack for debugging. } } public static String randomString(Random r, int n) { String s = ""; for(int i = 1; i <= n; i++) { s += (char)('!' + r.nextInt(11)); } return s; } }