首页 > 其他 > 详细

流转换成字符串

时间:2016-12-20 20:28:26      阅读:200      评论:0      收藏:0      [点我收藏+]
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;
    }
}

 

流转换成字符串

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!