欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

String、InputStream、OutputStream格式转换

程序员文章站 2024-03-24 12:20:58
...

1、String –> InputStream

InputStream stream = new ByteArrayInputStream(str.getBytes());

或者

ByteArrayInputStream stream= new ByteArrayInputStream(str.getBytes());

2、InputStream–>String

InputStreaminput;

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();