java截获控制台输出

kared 2012-12-21 09:54:58
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();这句话一直报错,小弟不知道是什么原因,希望解决下。
...全文
174 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
安特矮油 2012-12-25
  • 打赏
  • 举报
回复
看看异常是什么信息,然后再根据异常说的问题来解决
kared 2012-12-21
  • 打赏
  • 举报
回复
没人帮忙吗!!
霜之哀伤 2012-12-21
  • 打赏
  • 举报
回复
目测函数没有定义 话说这种错误,肯定有错误信息的,楼主看不懂?

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧