这是Sun的一个例子,看看吧。
RhymingWords.java:import java.io.*;class RhymingWords {
    public static void main(String[] args) {        try {
            DataInputStream words = new DataInputStream(new FileInputStream("words.txt"));                // do the reversing and sorting
            InputStream rhymedWords = reverse(sort(reverse(words)));                // write new list to standard out
            DataInputStream dis = new DataInputStream(rhymedWords);
            String input;            while ((input = dis.readLine()) != null) {
                System.out.println(input);
            }
            dis.close();        } catch (Exception e) {
            System.out.println("RhymingWords: " + e);
        }
    }    public static InputStream reverse(InputStream source) {
        PipedOutputStream pos = null;
        PipedInputStream pis = null;        try {
            DataInputStream dis = new DataInputStream(source);            pos = new PipedOutputStream();
            pis = new PipedInputStream(pos);
            PrintStream ps = new PrintStream(pos);            new WriteReversedThread(ps, dis).start();        } catch (Exception e) {
            System.out.println("RhymingWords reverse: " + e);
        }
        return pis;
    }    public static InputStream sort(InputStream source) {
        PipedOutputStream pos = null;
        PipedInputStream pis = null;        try {
            DataInputStream dis = new DataInputStream(source);            pos = new PipedOutputStream();
            pis = new PipedInputStream(pos);
            PrintStream ps = new PrintStream(pos);            new SortThread(ps, dis).start();        } catch (Exception e) {
            System.out.println("RhymingWords sort: " + e);
        }
        return pis;
    }
}

解决方案 »

  1.   

    SortThread.java:import java.io.*;class SortThread extends Thread {
        PrintStream ps;
        DataInputStream dis;    SortThread(PrintStream ps, DataInputStream dis) {
            this.ps = ps;
            this.dis = dis;
        }    public void run() {
             int MAXWORDS = 50;        if (ps != null && dis != null) {
                try {
                    String[] listOfWords = new String[MAXWORDS];
                    int numwords = 0, i = 0;
     
                    while ((listOfWords[numwords] = dis.readLine()) != null) {
                        numwords++;
                    }
                    quicksort(listOfWords, 0, numwords-1);
                    for (i = 0; i < numwords; i++) {
                        ps.println(listOfWords[i]);
                    }
                    ps.close();
                } catch (IOException e) {
                    System.out.println("WriteReversedThread run: " + e);
                }
            }
        }    protected void finalize() {
            try {
                if (ps != null) {
                    ps.close();
                    ps = null;
                }
                if (dis != null) {
                    dis.close();
                    dis = null;
                }
            } catch (IOException e) {
                System.out.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);
        }
    }
      

  2.   

    WriteReversedThread.java:import java.io.*;class WriteReversedThread extends Thread {
        PrintStream ps;
        DataInputStream dis;    WriteReversedThread(PrintStream ps, DataInputStream dis) {
            this.ps = ps;
            this.dis = dis;
        }    public void run() {
            if (ps != null && dis != null) {
                try {
                    String input;
                    while ((input = dis.readLine()) != null) {
                        ps.println(reverseIt(input));
                        ps.flush();
                    }
                    ps.close();
                } catch (IOException e) {
                    System.out.println("WriteReversedThread run: " + e);
                }
            }
        }    protected void finalize() {
            try {
                if (ps != null) {
                    ps.close();
                    ps = null;
                }
                if (dis != null) {
                    dis.close();
                    dis = null;
                }
            } catch (IOException e) {
                System.out.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();
        }
    }
    words.txt:
    anatomy
    animation
    applet
    application
    argument
    bolts
    class
    communicate
    component
    container
    development
    environment
    exception
    graphics
    image
    input
    integrate
    interface
    Java
    language
    native
    network
    nuts
    object
    output
    primer
    program
    security
    stream
    string
    threads
    tools
    user