How to convert String to InputStream in Java

In Java, we can use ByteArrayInputStream or IOUtils to convert a String into an InputStream

1. Pure Java – ByteArrayOutputStream


	InputStream result = new ByteArrayInputStream(anyString.getBytes(StandardCharsets.UTF_8));

2. Apache Commons IO – IOUtils.toInputStream

pom.xml

	<dependency>
		<groupId>commons-io</groupId>
		<artifactId>commons-io</artifactId>
		<version>2.6</version>
	</dependency>

	InputStream result = IOUtils.toInputStream(anyString, StandardCharsets.UTF_8);
Note
Read this to convert InputStream to String

1. Convert String To InputStream

StringToInputStream.java

package com.mkyong;

import org.apache.commons.io.IOUtils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class StringToInputStream {

    public static void main(String[] args) {

        String name = "mkyong";
        InputStream inputStream = convertStringToInputStream(name);

        // do something about the inputStream
    }

    // String -> InputStream
    private static InputStream convertStringToInputStream(String name) {

        InputStream result = new ByteArrayInputStream(name.getBytes(StandardCharsets.UTF_8));
        return result;

    }

    // commons-io
    private static InputStream convertStringToInputStreamCommonIO(String name) {

        InputStream result = IOUtils.toInputStream(name, StandardCharsets.UTF_8);
        return result;

    }

}

References

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
7 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
8 Comment authors
mkyongDanJon EmersonAbhinav TripathiOrhan Recent comment authors
newest oldest most voted
Dan
Guest
Dan

Why on earth are you going to and from InputStream here? String is character-based and Reader is character-based, so having InputStream (byte-based) is an intermediate step is generally a bad idea.

You just need to do the following:

BufferedReader br = new BufferedReader(new StringReader(str));

Jon Emerson
Guest
Jon Emerson

This solution is not at all efficient. It reallocates the string’s bytes to a new array only to satisfy API constraints. Technically, it’s very easy to implement the InputStream API on top of the String itself. And the Apache Commons I/O library does just that:

String source = “String to convert to an InputStream”;
InputStream in = IOUtils.toInputStream(source, “UTF-8”);

Abhinav Tripathi
Guest
Abhinav Tripathi

Thanks man… :)
This was the exact thing i was looking for…..

Orhan
Guest
Orhan

Thanks!

Jayanth
Guest
Jayanth

Thank you

Sanchez
Guest
Sanchez

Several issues here:

what about the encoding? If it is UTF-16 for instance then the default getBytes method will not work.
Furthermore, the end of lines will be removed…

Elana
Guest
Elana

How can you find a good homeowners insurajce policy.
Homeowners can easily maoe use of these insurances to protect them the houses.

the premium on your home insurance; be sure that yyou leet your agent.