The RhymingWords example highlights thePipedInputStream
andPipedOutputStream
classes. JDK 1.1 introduces two new classes,PipedReader
andPipedWriter
, that perform the same function asPipedInputStream
andPipedOutputStream
only they work with characters rather than bytes. Generally speaking, it is best to use the new character-streams. Similarly, the use of other byte streams in this example (such asFileReader
) should be changed to use the corresponding character-stream classes (such asFileInputStream
) instead.Also, if you try to compile the RhymingWords example featured in Using Streams to Implement Pipes using the JDK 1.1 compiler, you will find that it uses deprecated API. In particular,
DataInputStream.readLine
has been deprecated and so has creating aPrintStream
object.Here's the rhyming words example rewritten to use
Reader
andWriter
classes (in particular note the use ofPipedReader
andPipedWriter
in place ofPipedInputStream
andPipedOutputStream
) and to use non-deprecated API:WriteReversedThread.javaimport java.io.*; class RhymingWords { public static void main(String[] args) { try { FileReader words = new FileReader("words.txt"); // do the reversing and sorting Reader rhymedWords = reverse(sort(reverse(words))); // write new list to standard out BufferedReader br = new BufferedReader(rhymedWords); String input; PrintWriter stdout = new PrintWriter(System.out, true); while ((input = br.readLine()) != null) { stdout.println(input); } br.close(); } catch (Exception e) { System.err.println("RhymingWords: " + e); } } public static Reader reverse(Reader source) { PipedWriter pw = null; PipedReader pr = null; try { BufferedReader br = new BufferedReader(source); pw = new PipedWriter(); pr = new PipedReader(pw); PrintWriter output = new PrintWriter(pw); new WriteReversedThread(output, br).start(); } catch (Exception e) { System.err.println("RhymingWords reverse: " + e); } return pr; } public static Reader sort(Reader source) { PipedWriter pw = null; PipedReader pr = null; try { BufferedReader br = new BufferedReader(source); pw = new PipedWriter(); pr = new PipedReader(pw); PrintWriter output = new PrintWriter(pw); new SortThread(output, br).start(); } catch (Exception e) { System.err.println("RhymingWords sort: " + e); } return pr; } }SortThread.javaimport java.io.*; class WriteReversedThread extends Thread { PrintWriter pw; BufferedReader br; WriteReversedThread(PrintWriter pw, BufferedReader br) { this.pw = pw; this.br = br; } public void run() { if (pw != null && br != null) { try { String input; while ((input = br.readLine()) != null) { pw.println(reverseIt(input)); pw.flush(); } pw.close(); } catch (IOException e) { System.err.println("WriteReversedThread run: " + e); } } } protected void finalize() { try { if (pw != null) { pw.close(); pw = null; } if (br != null) { br.close(); br = null; } } catch (IOException e) { System.err.println("WriteReversedThread finalize: " + e); } } private String reverseIt(String source) { int i, len = source.length(); StringBuffer dest = new StringBuffer(len); for (i = (len - 1); i >= 0; i--) { dest.append(source.charAt(i)); } return dest.toString(); } }import java.io.*; class SortThread extends Thread { PrintWriter pw; BufferedReader br; SortThread(PrintWriter pw, BufferedReader br) { this.pw = pw; this.br = br; } public void run() { int MAXWORDS = 50; if (pw != null && br != null) { try { String[] listOfWords = new String[MAXWORDS]; int numwords = 0, i = 0; while ((listOfWords[numwords] = br.readLine()) != null) { numwords++; } quicksort(listOfWords, 0, numwords-1); for (i = 0; i < numwords; i++) { pw.println(listOfWords[i]); } pw.close(); } catch (IOException e) { System.err.println("WriteReversedThread run: " + e); } } } protected void finalize() { try { if (pw != null) { pw.close(); pw = null; } if (br != null) { br.close(); br = null; } } catch (IOException e) { System.err.println("WriteReversedThread finalize: " + e); } } private static void quicksort(String[] a, int lo0, int hi0) { int lo = lo0; int hi = hi0; if (lo >= hi) { return; } String mid = a[(lo + hi) / 2]; while (lo < hi) { while (lo<hi && a[lo].compareTo(mid) < 0) { lo++; } while (lo<hi && a[hi].compareTo(mid) > 0) { hi--; } if (lo < hi) { String T = a[lo]; a[lo] = a[hi]; a[hi] = T; } } if (hi < lo) { int T = hi; hi = lo; lo = T; } quicksort(a, lo0, lo); quicksort(a, lo == lo0 ? lo+1 : lo, hi0); } }