现在的位置: 首页 > 综合 > 正文

InputStream 、OutputStream与String的转换

2017年12月24日 ⁄ 综合 ⁄ 共 479字 ⁄ 字号 评论关闭

1.字符串转inputStream

String string;
//......
InputStream is = new ByteArrayInputStream(string.getBytes());

2.InputStream转字符串

ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i;
while ((i = is.read()) != -1) {
	baos.write(i);
}
String str = baos.toString();
System.out.println(str);

3.String写入OutputStream

OutputStream os = System.out;
os.write(string.getBytes());

4.OutputStream写入String

ByteArrayOutputStream baos = new ByteArrayOutputStream();
//向OutPutStream中写入,如 message.writeTo(baos);
String str = baos.toString();

【上篇】
【下篇】

抱歉!评论已关闭.