String、InputStream、OutputStream格式转换
1、String –> InputStream
InputStream stream = new ByteArrayInputStream(str.getBytes());
或者
ByteArrayInputStream stream= new ByteArrayInputStream(str.getBytes());
2、InputStream–>String
InputStream input;
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = input.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
out.toString();
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();
还没有评论,来说两句吧...