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);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }