今天在学习的时候用到了 InputStream 和 String 之间的转换 ,记录下来,以备查阅 ~
1. InputStream 转String
需要去读取输入流之中的字符,在拼接成字符串。
public String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "/n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
2. String 转 Is
InputStream in_withcode = new ByteArrayInputStream(str.getBytes( ** ));
** 代表编码格式,常用的是UTF-8