How do I convert a String to an InputStream?

John Zukowski

Since strings are character based, you should convert the String to a Reader-type stream, not an InputStream:

Reader reader = new StringReader(theString);

Note that the StringBufferInputStream doesn't work properly to make a String an InputStream as it doesn't properly handle converting characters into bytes. You can also try to work with a ByteArrayInputStream, as in new ByteArrayInputStream(theString.getBytes()), but it only works with platform's default character encoding.