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.

2 comments:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete