Android开发将流读取成String返回的工具类
在实际开发中经常需要将流读取成字符串,这里提供一个工具类实现:
public class StreamUtils {
/**
* 将流读取成String后返回
*
* @param is
* @return
* @throws IOException
*/
public static String readFromStream(InputStream is) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
String result = out.toString();
is.close();
out.close();
return result;
}
该方法传入流对象,输出String字符串,方便好用。