码迷,mamicode.com
首页 > 其他好文 > 详细

流转换成字符串

时间:2016-12-20 20:31:18      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:循环   pre   取数   stat   log   array   cat   bsp   input   

public class StreamUtil {
    /**
     * 流转换成字符串
     * @param is    流对象
     * @return        流转换成的字符串    返回null代表异常
     */
    public static String streamToString(InputStream is) {
        //1,在读取的过程中,将读取的内容存储值缓存中,然后一次性的转换成字符串返回
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        //2,读流操作,读到没有为止(循环)
        byte[] buffer = new byte[1024];
        //3,记录读取内容的临时变量
        int temp = -1;
        try {
            while((temp = is.read(buffer))!=-1){
                bos.write(buffer, 0, temp);
            }
            //返回读取数据
            return bos.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                is.close();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

 

流转换成字符串

标签:循环   pre   取数   stat   log   array   cat   bsp   input   

原文地址:http://www.cnblogs.com/xufengyuan/p/6203964.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!