码迷,mamicode.com
首页 > 其他好文 > 详细

baos bais 意义

时间:2015-11-20 19:03:50      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

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();
	}		
	
}

  

baos bais 意义

标签:

原文地址:http://www.cnblogs.com/freed0m/p/4981506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!