首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

java缴获控制台输出

2013-03-10 
java截获控制台输出package org.com.consoleTextAreaimport java.awt.TextAreaimport java.io.BufferedR

java截获控制台输出

package org.com.consoleTextArea;

import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.text.Document;

public class ConsoleTextArea extends TextArea {
public ConsoleTextArea(InputStream[] inStreams) {
for (int i = 0; i < inStreams.length; ++i)
startConsoleReaderThread(inStreams[i]);
} // ConsoleTextArea()

public ConsoleTextArea() throws IOException {
final LoopedStreams ls = new LoopedStreams();
// redirect System.out and System.err
PrintStream ps = new PrintStream(ls.getOutputStream());
System.setOut(ps);
System.setErr(ps);
startConsoleReaderThread(ls.getInputStream());
} // ConsoleTextArea()

private void startConsoleReaderThread(InputStream inStream) {
final BufferedReader br = new BufferedReader(new InputStreamReader(
inStream));
new Thread(new Runnable() {
public void run() {
StringBuffer sb = new StringBuffer();
try {
String s;
Document doc =getDocument();
while ((s = br.readLine()) != null) {
boolean caretAtEnd = false;
caretAtEnd = getCaretPosition() == doc.getLength() ? true
: false;
sb.setLength(0);
append(sb.append(s).append('\n').toString());
if (caretAtEnd)
setCaretPosition(doc.getLength());
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null,
"read from BufferedReader err:" + e);
System.exit(1);
}
}
}).start();
} // startConsoleReaderThread()
 // ConsoleTextArea
public static void main(String[] args) {
    JFrame f = new JFrame("ConsoleTextArea测试");
    ConsoleTextArea consoleTextArea = null;
    try {
        consoleTextArea = new ConsoleTextArea();
    }
    catch(IOException e) {
        System.err.println(
            "不能创建LoopedStreams:" + e);
        System.exit(1);
    }
    consoleTextArea.setFont(java.awt.Font.decode("monospaced"));
    f.getContentPane().add(new JScrollPane(consoleTextArea),
        java.awt.BorderLayout.CENTER);
    f.setBounds(50, 50, 300, 300);
    f.setVisible(true);
    f.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(
            java.awt.event.WindowEvent evt) {
            System.exit(0);


        }
    });
    // 启动几个写操作线程向
    // System.out和System.err输出
    startWriterTestThread(
        "写操作线程 #1", System.err, 920, 50);
    startWriterTestThread(
        "写操作线程 #2", System.out, 500, 50);
    startWriterTestThread(
        "写操作线程 #3", System.out, 200, 50);
    startWriterTestThread(
        "写操作线程 #4", System.out, 1000, 50);
    startWriterTestThread(
        "写操作线程 #5", System.err, 850,    50);
} // main()
private static void startWriterTestThread(
    final String name, final PrintStream ps, 
    final int delay, final int count) {
    new Thread(new Runnable() {
        public void run() {
            for(int i = 1; i <= count; ++i) {
                ps.println("***" + name + ", hello !, i=" + i);
                try {
                    Thread.sleep(delay);
                }
                catch(InterruptedException e) {}
            }
        }
    }).start();
} // startWriterTestThread()
} // ConsoleTextArea





package org.com.consoleTextArea;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class LoopedStreams {
private PipedOutputStream pipedOS =
  new PipedOutputStream();
 private boolean keepRunning = true;
 private ByteArrayOutputStream byteArrayOS =
  new ByteArrayOutputStream() {
  public void close() {
   keepRunning = false;
   try {
    super.close();
    pipedOS.close();
   }
   catch(IOException e) {
    
    System.exit(1);
   }
  }
 };

 private PipedInputStream pipedIS = new PipedInputStream() {
  public void close() {
   keepRunning = false;
   try {
    super.close();


   }
   catch(IOException e) {
    
    System.exit(1);
   }
  }
 };

 public LoopedStreams() throws IOException {
  pipedOS.connect(pipedIS);
  startByteArrayReaderThread();
 } // LoopedStreams()

 public InputStream getInputStream() {
  return pipedIS;
 } // getInputStream()

 public OutputStream getOutputStream() {
  return byteArrayOS;
 } // getOutputStream()

 private void startByteArrayReaderThread() {
  new Thread(new Runnable() {
   public void run() {
    while(keepRunning) {
     // check the number of bytes in the stream
     if(byteArrayOS.size() > 0) {
      byte[] buffer = null;
      synchronized(byteArrayOS) {
       buffer = byteArrayOS.toByteArray();
       byteArrayOS.reset(); // clear the buffer
      }
      try {
       // send the data to PipedOutputStream
       pipedOS.write(buffer, 0, buffer.length);
      }
      catch(IOException e) {
       // record the err or other things
       // for simple, we just exit here
       System.exit(1);
      }
     }
     else // no data to read, then the thread go to sleep
      try {
       // check the ByteArrayOutputStream for new data every one second
       Thread.sleep(1000);
      }
      catch(InterruptedException e) {}
     }
    }
  }).start();
 } // startByteArrayReaderThread()
} // LoopedStreams



这是我在网上找的关于java截获控制台输出的代码,关联了textarea,但是在代码中ConsoleTextArea类中Document doc =getDocument();这句话一直报错,小弟不知道是什么原因,希望解决下。

[解决办法]
目测函数没有定义
话说这种错误,肯定有错误信息的,楼主看不懂?

热点排行