Java Output流写入包装问题
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream); int size = byteArrayOutputStream.size(); System.out.println("写入之前为:" + size); outputStreamWriter.write("我是一个魔鬼"); // outputStreamWriter.flush(); // outputStreamWriter.close(); int size1 = byteArrayOutputStream.size(); System.out.println("写入之后为:" + size1);
当这样执行时,输出前后都为0,这是因为
OutputStreamWriter 相当于包装了一层,并没有给
ByteArrayOutputStream对象写入内容,需要
OutputStreamWriter 调用flush()或close()方法即可
复制请注明出处,在世界中挣扎的灰太狼