String和InputStream的转换

来源:互联网 发布:阿里云系统root工具 编辑:程序博客网 时间:2024/05/17 08:08

1. String --> InputStream

 

InputStream StringToInputStream(String str){
   ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
   return stream;
}

 

2. InputStream --> String

 

String inputStreamToString(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();
}

原创粉丝点击