Java面向对象程序设计第14章3-8和第15章6

Java面向对象程序设计第14章3-8和第15章6

3.完成下面方法中的代码,要求建立一个缓冲区,将字节输入流中的内容转为字符串。

import java.io.*;
public class test {
    static String loadStream(InputStream in) throws IOException {
        StringBuffer buffer = new StringBuffer();
        int count= 1,i=6;
        byte[] buf = new byte[count];//缓冲区
        while(i-->0) {
            in.read(buf,0,count);
            System.out.println(new String(buf));//按字节显示一下
            buffer.append(new String(buf));    //连接一个字符串
        }
        return new String(buffer);
    }

    public static void main(String[] args) throws IOException {
        InputStream in = new BufferedInputStream(System.in);//in必须要初始化一个对象,不能为null
        String test=loadStream(in);
        System.out.println("msg=: "+test);
    }
}
posted @ 2019-12-28 20:03  氢_氟_酸  阅读(160)  评论(0编辑  收藏  举报