java打印inputstream内容,从进程打印Java InputStream

基本上,从几乎没有什么信息,这听起来像你执行的过程,并从事件调度线程内读取InputStream 。

任何东西阻止EDT将阻止它处理重新绘制请求,相反,你应该使用像SwingWorker这样的function,允许你从EDT更新UI。

查看Swing中的并发性以获取更多详细信息

1FsuN.png

import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingWorker; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class PBDemo { public static void main(String[] args) throws Exception { new PBDemo(); } public PBDemo() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { public TestPane() { setLayout(new BorderLayout()); JTextArea ta = new JTextArea(); add(new JScrollPane(ta)); new ProcessWorker(ta).execute(); } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } } public interface Consumer { public void consume(String value); } public class ProcessWorker extends SwingWorker implements Consumer { private JTextArea textArea; public ProcessWorker(JTextArea textArea) { this.textArea = textArea; } @Override protected void process(List chunks) { for (String value : chunks) { textArea.append(value); } } @Override protected Integer doInBackground() throws Exception { // Forced delay to allow the screen to update Thread.sleep(5000); publish("Starting...\n"); int exitCode = 0; ProcessBuilder pb = new ProcessBuilder("java.exe", "-jar", "HelloWorld.jar"); pb.directory(new File("C:\\DevWork\\personal\\java\\projects\\wip\\StackOverflow\\HelloWorld\\dist")); pb.redirectError(); try { Process pro = pb.start(); InputConsumer ic = new InputConsumer(pro.getInputStream(), this); System.out.println("...Waiting"); exitCode = pro.waitFor(); ic.join(); System.out.println("Process exited with " + exitCode + "\n"); } catch (Exception e) { System.out.println("sorry" + e); } publish("Process exited with " + exitCode); return exitCode; } @Override public void consume(String value) { publish(value); } } public static class InputConsumer extends Thread { private InputStream is; private Consumer consumer; public InputConsumer(InputStream is, Consumer consumer) { this.is = is; this.consumer = consumer; start(); } @Override public void run() { try { int in = -1; while ((in = is.read()) != -1) { // System.out.print((char) in); consumer.consume(Character.toString((char)in)); } } catch (IOException exp) { exp.printStackTrace(); } } } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值