home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / io / example-1.1 / RhymingWords.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.5 KB  |  81 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.io.*;
  18.  
  19. class RhymingWords {
  20.     public static void main(String[] args) {
  21.  
  22.         try {
  23.             FileReader words = new FileReader("words.txt");
  24.  
  25.                 // do the reversing and sorting
  26.             Reader rhymedWords = reverse(sort(reverse(words)));
  27.  
  28.                 // write new list to standard out
  29.             BufferedReader br = new BufferedReader(rhymedWords);
  30.             String input;
  31.         PrintWriter stdout = new PrintWriter(System.out, true);
  32.  
  33.             while ((input = br.readLine()) != null) {
  34.                 stdout.println(input);
  35.             }
  36.             br.close();
  37.  
  38.         } catch (Exception e) {
  39.             System.err.println("RhymingWords: " + e);
  40.         }
  41.     }
  42.  
  43.     public static Reader reverse(Reader source) {
  44.         PipedWriter pw = null;
  45.         PipedReader pr = null;
  46.  
  47.         try {
  48.             BufferedReader br = new BufferedReader(source);
  49.  
  50.             pw = new PipedWriter();
  51.             pr = new PipedReader(pw);
  52.             PrintWriter output = new PrintWriter(pw);
  53.  
  54.             new WriteReversedThread(output, br).start();
  55.  
  56.         } catch (Exception e) {
  57.             System.err.println("RhymingWords reverse: " + e);
  58.         }
  59.         return pr;
  60.     }
  61.  
  62.     public static Reader sort(Reader source) {
  63.         PipedWriter pw = null;
  64.         PipedReader pr = null;
  65.  
  66.         try {
  67.             BufferedReader br = new BufferedReader(source);
  68.  
  69.             pw = new PipedWriter();
  70.             pr = new PipedReader(pw);
  71.             PrintWriter output = new PrintWriter(pw);
  72.  
  73.             new SortThread(output, br).start();
  74.  
  75.         } catch (Exception e) {
  76.             System.err.println("RhymingWords sort: " + e);
  77.         }
  78.         return pr;
  79.     }
  80. }
  81.