Java中String与InputStream互相转换代码

1、String 转成 InputStream

//str1为待转换的字符串
InputStrem inputStrem = new ByteArrayInputStream(str1.getBytes());  

或者

ByteArrayInputStream byteArrayInputStream= new ByteArrayInputStream(str1.getBytes());  

2、InputStream转成String

InputStream inputStream = null;  

StringBuffer stringBuffer = new StringBuffer();  
     byte[] byt = new byte[4096];  
     for (int i; (i = input.read(byt)) != -1;) {  
          stringBuffer.append(new String(byt, 0, i));  
     }  
return stringBuffer.toString();  

你可能感兴趣的:(Java)