home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / io / example / RhymingWords.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.5 KB  |  80 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.             DataInputStream words = new DataInputStream(new FileInputStream("words.txt"));
  24.  
  25.                 // do the reversing and sorting
  26.             InputStream rhymedWords = reverse(sort(reverse(words)));
  27.  
  28.                 // write new list to standard out
  29.             DataInputStream dis = new DataInputStream(rhymedWords);
  30.             String input;
  31.  
  32.             while ((input = dis.readLine()) != null) {
  33.                 System.out.println(input);
  34.             }
  35.             dis.close();
  36.  
  37.         } catch (Exception e) {
  38.             System.out.println("RhymingWords: " + e);
  39.         }
  40.     }
  41.  
  42.     public static InputStream reverse(InputStream source) {
  43.         PipedOutputStream pos = null;
  44.         PipedInputStream pis = null;
  45.  
  46.         try {
  47.             DataInputStream dis = new DataInputStream(source);
  48.  
  49.             pos = new PipedOutputStream();
  50.             pis = new PipedInputStream(pos);
  51.             PrintStream ps = new PrintStream(pos);
  52.  
  53.             new WriteReversedThread(ps, dis).start();
  54.  
  55.         } catch (Exception e) {
  56.             System.out.println("RhymingWords reverse: " + e);
  57.         }
  58.         return pis;
  59.     }
  60.  
  61.     public static InputStream sort(InputStream source) {
  62.         PipedOutputStream pos = null;
  63.         PipedInputStream pis = null;
  64.  
  65.         try {
  66.             DataInputStream dis = new DataInputStream(source);
  67.  
  68.             pos = new PipedOutputStream();
  69.             pis = new PipedInputStream(pos);
  70.             PrintStream ps = new PrintStream(pos);
  71.  
  72.             new SortThread(ps, dis).start();
  73.  
  74.         } catch (Exception e) {
  75.             System.out.println("RhymingWords sort: " + e);
  76.         }
  77.         return pis;
  78.     }
  79. }
  80.