本帖最后由 王金科 于 2012-8-23 15:38 编辑
- import java.io.*;
- public class ByteArrayStreamDemo
- {
- public static void main(String[] args)
- {
- //数据源
- ByteArrayInputStream bis = new ByteArrayInputStream("abcdef".getBytes());
- //数据目的地
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
-
- int by = 0;
- while((by=bis.read())!=-1)
- {
- bos.write(by);
- //System.out.println(bos);
- }
-
- System.out.println(bos.size());
- System.out.println(bos.toString());//System.out.println(bos);
- }
- }
- System.out.println(bos.toString());//System.out.println(bos);
- 这里有没有toString打印的结果都一样,那么有什么本质的区别,会产生什么影响?
- import java.io.CharArrayReader;
- import java.io.CharArrayWriter;
- import java.io.IOException;
- public class CharArrayReaderDemo {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- CharArrayReader cr = new CharArrayReader("ASasdsadas".toCharArray());
- CharArrayWriter cw = new CharArrayWriter();
-
- int len = 0;
- while((len=cr.read())!=-1){
- cw.write(len);
- //System.out.println(cw);
- }
- System.out.println(cw.size());
- System.out.println(cw.toString());//System.out.println(cw);
- }
- }
- 这段代码也一样,有没有toString打印的结果都一样,那么有什么本质的区别,会产生什么影响?
复制代码 |