String和InputStream的轉換

String和InputStream的轉換

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章