标签:
import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import org.junit.Test; public class testIO { ByteArrayOutputStream baos = new ByteArrayOutputStream(); @Test public void test1() throws Exception { Process exec = Runtime.getRuntime().exec("ipconfig"); InputStream inputStream = exec.getInputStream(); byte[] bytes = new byte[1024]; int len = 0; // ByteArrayInputStream bais = new while((len=inputStream.read(bytes))!=-1) { // inputStream.read(bytes); baos.write(bytes,0,len); } baos.flush(); Thread t1 = new Thread(new Runnable() { byte[] temp = new byte[1024]; byte[] byteArray = baos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(byteArray); BufferedInputStream bis = new BufferedInputStream(bais); int len=0; StringBuilder sb = new StringBuilder(); public void run() { // TODO Auto-generated method stub try { while((len=bis.read(temp))!=-1) { sb.append(new String(temp,0,len)); } System.out.println(sb.toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); Thread t2 = new Thread(new Runnable() { byte[] temp = new byte[1024]; byte[] byteArray = baos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(byteArray); BufferedInputStream bis = new BufferedInputStream(bais); int len=0; StringBuilder sb = new StringBuilder(); public void run() { // TODO Auto-generated method stub try { while((len=bis.read(temp))!=-1) { sb.append(new String(temp,0,len)); } System.out.println(sb.toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); t1.start(); t2.start(); } }
标签:
原文地址:http://www.cnblogs.com/freed0m/p/4981506.html