入题所述,

解决方案 »

  1.   

    不懂....
    可不可以将Area的内容装到一个OutputStream中?之后用这个进行输出?
    如何将一个OutputStream定为默认输出流,等待高手 
      

  2.   

    jtextOut = new OutputStream() {

    final int BUFFER_LENGTH = 1024;
    byte buf[] = new byte[BUFFER_LENGTH];
    int pos = 0;
    public void write(int b) throws IOException {
    buf[pos ++] = (byte)b;
    if (pos >= BUFFER_LENGTH)
    flush();
    }

    public void flush() throws IOException
    {
    if (pos >= BUFFER_LENGTH)
    txtLog.append(new String(buf));
    else
    txtLog.append(new String(buf, 0, pos));
    pos = 0;
    }
    };
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
    jbInit();
    } catch (Exception e) {
    e.printStackTrace();
    }
    //output message to textarea
    System.setOut(new PrintStream(jtextOut, true));
      

  3.   

    PrintStream ps = new PrintStream(new OutputStream(){
                
                final int LENGTH = 256;
                byte[] bb = new byte[LENGTH]; 
                int p = 0;
                public void write(int b) throws IOException {
                    bb[p++] = (byte)b;
                    
                    if(p >= LENGTH){
                        flush();
                    }
                }
                
                public void flush()throws IOException{
                    String str = new String(bb,0,p);
                    
                    jTextArea1.append(str);
                    
                    p = 0;
                }
            },true);
            
            System.setOut(ps);
            
            System.out.println("test str"};
      

  4.   

    import javax.swing.*;
    import java.awt.*;
    import javax.swing.event.*;
    import java.io.*;
    import java.awt.event.*;class MyPipe implements Runnable{
      private JTextArea textarea;
      private PipedInputStream pis = new PipedInputStream();
      private PipedOutputStream pos;
      private BufferedReader reader = new BufferedReader(
          new InputStreamReader(pis));
      private Thread thread = null;
      
      public MyPipe(JTextArea ta) throws IOException{
        textarea = ta;
        pos = new PipedOutputStream(pis);
        thread = new Thread(this);
        thread.start();
      }
      
      public PipedOutputStream getOut(){
        return pos;
      }
      
      public void run(){
        String line = null;
        while(thread == Thread.currentThread()){
          try{
            line = reader.readLine();
          }      
          catch(IOException ioe){
            break;
          }
          
          if(line == null){
            break;
          }
          else{
            final String s = line;
            SwingUtilities.invokeLater(new Runnable(){
              public void run(){
                textarea.append(s);
                textarea.append(System.getProperty("line.separator", "\n\r"));
              }
            });
          }
        }
      }
      
      public void close(){
        thread = null;
      }
    }public class Sink extends JFrame{
      private MyPipe pipe;
      
      public Sink() throws IOException{
        JTextArea area = new JTextArea(10, 40);
        area.addMouseListener(new MouseAdapter(){
          public void mouseClicked(MouseEvent e){
            System.out.println(e.getX() + ", " + e.getY());
          }
        });
        
        JScrollPane scroll = new JScrollPane(area);
        pipe = new MyPipe(area);
        System.setOut(new PrintStream(pipe.getOut(), true));
        this.getContentPane().add(scroll, BorderLayout.CENTER);
        this.pack();
        this.setLocation(200, 200);
        this.setVisible(true);
      }
      
      public void processWindowEvent(WindowEvent e){
        super.processWindowEvent(e);
        if(e.getID() == e.WINDOW_CLOSING){
          pipe.close();
          System.exit(0);
        }
      }
      
      public static void main(String[] args)throws IOException{
        Sink sink = new Sink();
      }
    }
      

  5.   

    updn(快乐编程) 的办法比我的简单。自己写了个OutputStream,挺好。我本来想继承一个,但是访问不到它的buffer,所以才用了piped。有点建议:
    1 每次write之后直接flush,而不要等到buffer满了之后
    2 OutputStream的几个write方法都应该覆盖