How to convert String to InputStream in Java

In Java, we can use ByteArrayInputStream to convert a String to an InputStream.


String str = "mkyong.com";
InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));

Table of contents

1. ByteArrayInputStream

This example uses ByteArrayInputStream to convert a String to an InputStream and saves it into a file.

StringToInputStream.java

package com.mkyong.string;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class ConvertStringToInputStream {

    public static final int DEFAULT_BUFFER_SIZE = 8192;

    public static void main(String[] args) throws IOException {

        String name = "mkyong.com";

        // String to InputStream
        InputStream is = new ByteArrayInputStream(name.getBytes(StandardCharsets.UTF_8));

        // save to a file
        save(is, "c:\\test\\file.txt");

    }

    // save the InputStream to a File
    private static void save(final InputStream is, final String fileName)
            throws IOException {

        // read bytes from InputStream and write it to FileOutputStream
        try (FileOutputStream outputStream =
                     new FileOutputStream(new File(fileName), false)) {
            int read;
            byte[] bytes = new byte[DEFAULT_BUFFER_SIZE];
            while ((read = is.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
        }

    }

}

2. Apache Commons IO – IOUtils

This example uses commons-io library, IOUtils.toInputStream API to convert String to InputStream.

pom.xml

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

import org.apache.commons.io.IOUtils;

// String to InputStream
InputStream result = IOUtils.toInputStream(str, StandardCharsets.UTF_8);

Review the source code of IOUtils.toInputStream, it is using the same ByteArrayInputStream to convert a String to an InputStream.


package org.apache.commons.io;

public class IOUtils {

	public static InputStream toInputStream(final String input, final Charset charset) {
		return new ByteArrayInputStream(input.getBytes(Charsets.toCharset(charset)));
	}

	//...
}

3. Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-string

or

$ cd java-io

4. References

7 comments on “How to convert String to InputStream in Java

  1. 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));

    Reply
    1. For example, we need to convert String or any character-based stream to bytes to send over the network or Internet. And many APIs accept only InputStream.

      Reply
  2. 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”);

    Reply
  3. Thanks man… 🙂
    This was the exact thing i was looking for…..

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *