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

≡放荡痞女 提交于 2019-12-28 20:11:24

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);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!