将进程输出重定向到stdout

将进程输出重定向到stdout,第1张

进程输出重定向到stdout

它使用一个类读取执行的程序生成的所有输出,并将其显示在其自己的stdout中。

class StreamGobbler extends Thread {    InputStream is;    // reads everything from is until empty.     StreamGobbler(InputStream is) {        this.is = is;    }    public void run() {        try { InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line=null; while ( (line = br.readLine()) != null)     System.out.println(line); } catch (IOException ioe) { ioe.printStackTrace();          }    }}Runtime rt = Runtime.getRuntime();Process proc = rt.exec("javac");//output both stdout and stderr data from proc to stdout of this processStreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream());StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream());errorGobbler.start();outputGobbler.start();proc.waitFor();


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5428826.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存