Java中String和InputStream的转换

来源:互联网 发布:java中自定义异常类 编辑:程序博客网 时间:2024/06/05 10:55

1. String --> InputStream
InputStream String2InputStream(String str){
   ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
   return stream;
}

2. InputStream --> String
String inputStream2String(InputStream is){
   BufferedReader in = new BufferedReader(new InputStreamReader(is));
   StringBuffer buffer = new StringBuffer();
   String line = "";
   while ((line = in.readLine()) != null){
     buffer.append(line);
   }
   return buffer.toString();
}

 

 

转自http://jn1981.blog.163.com/blog/static/538645020106229139894/

原创粉丝点击