How can I read a String into an inputStream in Java?

Possible duplicate:
How to convert String to InputStream in Java?

How can I read a String into an InputStream in Java?

I want to be able to convert String say = "say"

to InputStream / InputSource. How to do it?

+3


source to share


4 answers


public class StringToInputStreamExample {
    public static void main(String[] args) 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();
   }
}

      



Source: How to Convert String to InputStream in Java

+4


source


Something like...

InputStream is = new ByteArrayInputStream(sValue.getBytes());

      



Must work...

+2


source


You can use ByteArrayInputStream

. It reads elements from byte[]

using methods InputStream

.

0


source


For InputStream

MadProgrammer has the answer.

If a Reader

is ok, you can use:

Reader r = StringReader(say);

      

0


source







All Articles