Today we will look into how to convert String to InputStream in java. Recently I wrote a post to convert InputStream to String.

Java String to InputStream

There are two ways that I have used to convert String to InputStream.

  1. Java IO ByteArrayInputStream class
  2. Apache Commons IO IOUtils class

Let’s have a look at example program to use these classes.

Java String to InputStream using ByteArrayInputStream

String to InputStream using Apache Commons IOUtils

You can use IOUtils if you are already using Apache Commons IO jars, otherwise there is no benefit since internally it’s using ByteArrayInputStream class. Below is the toInputStream method implementation from IOUtils class source code.

That’s all about converting String to Input Stream in java.

By admin

Leave a Reply

%d bloggers like this: