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(); } }
时间: 2024-10-10 21:34:53