2
A
回答
2
你可以得到下面的代碼片段shell命令的輸出,在while循環設置的JTextArea。
try {
String cmd = "java"; // Set shell command
Process child = Runtime.getRuntime().exec(cmd);
InputStream lsOut = child.getInputStream();
InputStreamReader r = new InputStreamReader(lsOut);
BufferedReader in = new BufferedReader(r);
// read the child process' output
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
// You should set JtextArea
}
} catch (Exception e) { // exception thrown
System.err.println("Command failed!");
}
+0
我認爲你的代碼示例需要改變來處理Swing線程問題。 – 2011-04-17 13:48:33
2
您需要讀取標準輸出作爲進程輸入流並使用另一個線程的事件隊列更新JTextArea。請參見下面的示例代碼:
public class OutputDisplayer implements Runnable {
protected final JTextArea textArea_;
protected Reader reader_ = null;
public OutputDisplayer(JTextArea textArea) {
textArea_ = textArea;
}
public void commence(Process proc) {
InputStream in = proc.getInputStream();
reader_ = new InputStreamReader(in);
Thread thread = new Thread(this);
thread.start();
}
public void run() {
StringBuilder buf = new StringBuilder();
try {
while(reader_ != null) {
int c = reader_.read();
if(c==-1) return;
buf.append((char) c);
setText(buf.toString());
}
} catch (IOException ioe) {
buf.append("\n\nERROR:\n"+ioe.toString());
setText(buf.toString());
} finally {
try {
reader_.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
private void setText(final String text) {
EventQueue.invokeLater(new Runnable() {
public void run() {
textArea_.setText(text);
}
});
}
}
+0
調用.setText()似乎消耗大量內存。請看這個,因爲我正在請求幫助。謝謝。 http://stackoverflow.com/questions/11302982/jtextarea-consumes-a-lot-of-memory – jobobo 2012-07-03 07:52:31
2
除了在我的評論我的兩個環節上面,我創建並使用了TextAreaOutputStream,有助於輸出流數據重定向到一個textarea:
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class TextAreaOutputStream extends OutputStream {
private final JTextArea textArea;
private final StringBuilder sb = new StringBuilder();
private String title;
public TextAreaOutputStream(final JTextArea textArea, String title) {
this.textArea = textArea;
this.title = title;
sb.append(title + "> ");
}
@Override
public void flush() {
}
@Override
public void close() {
}
@Override
public void write(int b) throws IOException {
if (b == '\r')
return;
if (b == '\n') {
final String text = sb.toString() + "\n";
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(text);
}
});
sb.setLength(0);
sb.append(title + "> ");
}
sb.append((char) b);
}
}
相關問題
- 1. 如何在mongo shell中顯示每頁命令輸出頁面
- 2. 如何在php中輸出shell命令的輸出?
- 3. 在下一個shell提示符後顯示Unix命令輸出
- 4. NSTextView的Shell命令輸出
- 5. 在Java JTextArea中顯示Unicode
- 6. 管道輸出命令shell命令
- 7. 如何在C#中顯示SQL「PRINT」命令的輸出?
- 8. 如何在shell中逐行處理命令的輸出?
- 9. 如何在shell腳本中獲取sqlite命令的輸出
- 10. emacs shell命令輸出不顯示ANSI顏色,但代碼
- 11. 如何在bash中連續顯示命令輸出?
- 12. 如何在erlang中連續顯示os命令輸出?
- 13. 在Perl中輸出shell命令的輸出
- 14. exec()命令沒有在標準輸出中顯示輸出
- 15. Java - 如何訪問JTextArea中突出顯示的材料
- 16. 如何在PHP中獲取「at」shell命令輸出?
- 17. 如何在UNIX shell中打印輸出命令行數
- 18. 如何在可可中運行shell命令並獲取輸出?
- 19. 如何從Android shell中的PS命令輸出中獲取PID
- 20. 如何在輸入每個命令後在python pdb中顯示'l'的輸出
- 21. 如何在stdout上顯示Linux命令的輸出並將其管道傳輸到另一個命令?
- 22. 得到一個shell命令的輸出
- 23. Shell - 禁止單個命令的輸出
- 24. 如何在Windows中隨時捕獲命令提示符的顯示輸出?
- 25. 在Django WebApp上實時查看shell命令的輸出
- 26. 如何顯示執行的命令行程序的輸出?
- 27. Groovy不在命令行上顯示輸出
- 28. 在網頁上使用Perl顯示命令輸出cgi
- 29. 在txt文件上顯示php輸出Linux命令行
- 30. 將兩個shell命令合併到shell中的一個輸出
請有看看回答你的問題的兩個不錯的鏈接:[Redirect-output-stderr-stdout-JTextArea](http://www.coderanch.com/t/458147/GUI/java/Redirect-output-stderr-stdout-JTextArea)和這個經典:[當Runtime.exec()不會](http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html)。希望這可以幫助! – 2011-04-17 13:43:48