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

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 17:29   1930   0

/**

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

*

* @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;

}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP