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.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact