Wednesday, February 29, 2012

Java : How To Convert String Into InputStream ?

    public static InputStream stringToInputStream(String str) throws IOException {
           // String str = "This is a String ~ GoGoGo";
            
                    // convert String into InputStream
                    InputStream is = new ByteArrayInputStream(str.getBytes());
            
                    // read it with BufferedReader
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
            
                    String line;
                    while ((line = br.readLine()) != null) {
                            System.out.println(line);
                    }
            
                    br.close();
                    return is;
        }