如何读取控制台输出 [英] How to read console ouput
问题描述
使用Grails框架如何阅读控制台输出。我的意思是我有一个java库在我的grails应用程序,输出(system.out.println(x))到控制台,我想显示在我的grails应用程序的文本框中。我如何去做这个。
Using the Grails framework how would one go about reading console output. What I mean is I have a java library in my grails app that outputs(system.out.println("x")) to the console and I want to display this in a textbox in my grails app. How do I go about doing this.
我不想将消息写入文件并以这种方式读取数据。是否有直接从控制台读取。
I dont want to have to write the messages out to a file and read them in that way or to a database. Is there someway of directly reading from the console.
提前感谢。
EDIT **
它是我使用的第三方jar文件
Its a third party jar file that I am using
推荐答案
输出与System.setOut()方法。下面是一个快速示例,在五秒后将System.out重定向到文本窗格:
You can redirect the output with the System.setOut() method. Here is a quick example that redirects System.out to a text pane after five seconds:
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Date;
public class TextCapture {
public static void main(String[] args) throws Exception {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final JTextArea jta = new JTextArea();
jta.setPreferredSize(new Dimension(400, 200));
jf.getContentPane().add(jta);
jf.pack();
jf.setVisible(true);
OutputStream textOS = new OutputStream() {
@Override
public void write(int b) throws IOException {
jta.setText(jta.getText() + ((char)b));
}
};
for(int i = 0; i < 20; i++) {
Thread.sleep(1000);
if(i == 5) {
System.setOut(new PrintStream(textOS));
}
System.out.println(String.valueOf(new Date()));
}
}
}
这篇关于如何读取控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!