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

输入流和字符串互转,InputStream2String,String2InputStream

时间:2016-08-01 12:03:08      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

输入流转字符串

public static String InputStream2String(InputStream in) {
	InputStreamReader reader = null;
	try {
		reader = new InputStreamReader(in, "UTF-8");
	} catch (UnsupportedEncodingException e1) {
		e1.printStackTrace();
	}
	BufferedReader br = new BufferedReader(reader);
	StringBuilder sb = new StringBuilder();
	String line = "";
	try {
		while ((line = br.readLine()) != null) {
			sb.append(line);
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
	return sb.toString();
}

  

字符串转输入流

public static InputStream String2InputStream(String str) {
	ByteArrayInputStream stream = null;
	try {
		stream = new ByteArrayInputStream(str.getBytes("UTF-8"));
	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	}
	return stream;
}

  

输入流和字符串互转,InputStream2String,String2InputStream

标签:

原文地址:http://www.cnblogs.com/nihaorz/p/5725224.html

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