Java读取流并拼接转换成字符串

/** * 读取流并拼接转换成字符串 * * @param is * 流 * @param encode * 字符集编码 null的时候为gb2312 * @return 转换后的字符串 * @throws IOExcept...
/**
 * 读取流并拼接转换成字符串
 * 
 * @param is
 *            流
 * @param encode
 *            字符集编码 null的时候为gb2312
 * @return 转换后的字符串
 * @throws IOException
 */
public static  String readInputStream(InputStream is, String encode)
		throws IOException {
	if (encode == null) {
		encode = "gb2312";
	}
	BufferedReader in = null;
	String res = "";
	try{
		in = new BufferedReader(new InputStreamReader(is, encode));
		StringBuffer buffer = new StringBuffer();
		String line;
		while ((line = in.readLine()) != null) {
			buffer.append(line);
		}
		res = buffer.toString();
	}catch(Exception e){
		//异常处理
	}finally{
		is.close();
	}
        return res;
}

  • 发表于 2017-06-27 22:29
  • 阅读 ( 1219 )
  • 分类:Java基础

2 条评论

请先 登录 后评论
不写代码的码农
三叔

66 篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除