Tuesday, November 11, 2008

Convert String to InputStream

If you want to create InputStream from String in Java, the following code might help.

String text = "simple string";
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( text.getBytes());

Since ByteArrayInputStream is a concrete class extends from InputStream, you can instantiate it using the new keyword and use it in place where InputStream is expected i.e. use it as an argument passed to another method.

1 comment: