Java将InputStream转换为String


Java将InputStream转换为String

import java.io.BufferedReader;

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;



public class ISString {

   public static void main(String[] args) {

      InputStreamReader isr = null;

      BufferedReader br = null;

      InputStream is = new ByteArrayInputStream("hi there..we are doing java".getBytes());



      StringBuilder sb = new StringBuilder();

      String con;

      try {

         isr = new InputStreamReader(is);

         br = new BufferedReader(isr);



         while ((con = br.readLine()) != null) {

            sb.append(con);

         }

      } catch (IOException ioe) {

         System.out.println("IO Exception occurred");

         ioe.printStackTrace();

      }

      String s = sb.toString();

      System.out.println(s);

   }

}