Welcom to RO_wsy's blog

java中ByteArrayInputStream和ByteArrayOutputStream类用法

ByteArrayInputStream和ByteArrayOutputStream,用于以IO流的方式来完成对字节数组内容的读写,来支持类似内存虚拟文件或者内存映射文件的功能

实例:

import java.io.*;

public class ByteArrayStreamTest {
	public static void main(String [] args) {
		String str = "abcdef";
		
		ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes());
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		
		transform(in, out);
		
		byte[] result = out.toByteArray();
		
		System.out.println(out);
		System.out.println(new String(result));
		
		transform(System.in, System.out); // 从键盘读,输出到显示器
	}
	
	public static void transform(InputStream in, OutputStream out) {
		int ch = 0;
		
		try {
			while ((ch = in.read()) != -1) {
				int upperChar = Character.toUpperCase((char)ch);
				out.write(upperChar);
			} // close while
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


 

posted @ 2012-11-21 14:33  RO_wsy  阅读(285)  评论(0编辑  收藏  举报