3

如何使用 Java Swing 实现管道连接器,如 Yahoo 管道。或者 Java Swing 中的任何其他类型的接线,就此而言。

4

1 回答 1

5

下面是一个在 java中使用PipedReaderand的例子:PipedWriter

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedReader;
import java.io.PipedWriter;
/**
 * @date 1st May, 2011
 * @author sanju.org
 *
 * To demonstrate the use of piped character streams.
 * Example program for piped character stream in java.
 *
 */

public class PipedReaderExample {

    public static void main(String args[]) throws IOException{
        PipedReader reader = new PipedReader();
        PipedWriter writer = new PipedWriter(reader);
        Thread readerThread = new Thread(new ReaderThread(writer));
        Thread writerThread = new Thread(new WriterThread(reader));
        readerThread.start();
        writerThread.start();
    }

}

class ReaderThread implements Runnable{
    PipedWriter writer;
    public ReaderThread(PipedWriter writer){
        this.writer = writer;
    }

    public void run() {
        InputStreamReader streamReader = new InputStreamReader(System.in);
        BufferedReader bufferedReader = new BufferedReader(streamReader);
        try {
            while (true) {
                //sample implementation reading from console
                //real implementation can be reading from a socket or a file
                //or from server side code
                String line = bufferedReader.readLine();
                writer.write(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class WriterThread implements Runnable{
    PipedReader reader;
    public WriterThread(PipedReader reader){
        this.reader = reader;
    }

    public void run() {
        while(true){
            try {
                char c;
                while( (c = (char)reader.read()) != -1){
                    //write your business logic here
                    //could be writing into a file
                    //could be processing the date
                    System.out.println(c);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

至于在PipedReader/Writer课堂上使用雅虎,我猜你会阅读他们的 API 来连接到 url 等

以下是与PipedReaderPipedWriter(教程和信息的组合)相关的其他链接列表,可能会有所帮助:

至于 Swing 方面,要显示从管道接收到的数据,您需要使用TextComponent其中一个:

每个都有一个setText(...)允许您设置其内容的

参考:

于 2012-10-23T20:47:12.167 回答