首页 > 解决方案 > 如何将 CMD 的输出逐行导入 JTextpane

问题描述

我有 textField 我在其中编写 cmd 命令,我想从导入 JtextPane 的 CMD 中获取输出结果。我明白了,但是在我执行一些命令后,整个输出会立即导入到文本窗格中,我想逐行写入,就像在 CMD 控制台中写入一样。

感谢帮助

String input = textField.getText().toString();
try {
    Process p = Runtime.getRuntime().exec("cmd /c" + " " + input);

    BufferedReader StdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String s;
    while ((s = StdInput.readLine()) != null) {

        try {
            Document doc = textPane.getDocument();
            doc.insertString(doc.getLength(), s + "\n", null);
        } catch (BadLocationException exc) {
            exc.printStackTrace();
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}

标签: javabufferedreaderjtextpaneinputstreamreader

解决方案


推荐阅读