1057

Given a string:

String exampleString = "example";

How do I convert it to an InputStream?

1
  • 2
    InputStreams look at bytes, Readers look at characters. Commented Mar 28, 2021 at 18:11

5 Answers 5

1662

Like this:

InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));

Note that this assumes that you want an InputStream that is a stream of bytes that represent your original string encoded as UTF-8.

For versions of Java less than 7, replace StandardCharsets.UTF_8 with "UTF-8".

6
  • 2
    Doesn't this charset get lost when doing: String --> bytes --> ByteArrayInputStream ? When trying this on a for example "ü", the bytes will have the right letter, but the ByteArrayInputStream will have lost the right conversion. Or am I wrong? Commented Sep 18, 2013 at 11:28
  • 22
    StandardCharsets requires minimum API level 19. Commented Jan 30, 2014 at 15:05
  • 3
    @JonathanRamos it's not up to the stream to hold the conversion. It's up to whatever is decoding the bytes back into strings. Commented Mar 12, 2014 at 18:28
  • 27
    @Nantoka Instead of StandardCharsets.UTF_8 you can use Charset.forName("UTF-8") for any API Level. Commented Dec 24, 2014 at 9:19
  • 1
    Not pretty if you have large string objects and want to save on memory/allocations. A proper streaming solution would be nice. Commented Jan 31, 2023 at 23:25
297

I find that using Apache Commons IO makes my life much easier.

String source = "This is the source of my input stream";
InputStream in = org.apache.commons.io.IOUtils.toInputStream(source, "UTF-8");

You may find that the library also offer many other shortcuts to commonly done tasks that you may be able to use in your project.

7
  • 7
    They used new ByteArrayInputStream(exampleString.getBytes("UTF-8")). So it will be optimized way to use InputStream stream = new ByteArrayInputStream(exampleString.getBytes("UTF-8")); Commented Aug 24, 2011 at 12:34
  • 11
    @PankajKumar: Java's JIT compiler is more than able to inline this. Commented Jul 20, 2012 at 12:14
  • 9
    Using a method which doesn't specify encoding is a terrible idea... Commented Jun 5, 2013 at 2:01
  • 2
    @b1naryatr0phy: Apache commons includes another form of this method which takes the encoding as a second parameter (which, you're right, is preferable): InputStream in = IOUtils.toInputStream(source, "UTF-8"); Commented Jan 11, 2014 at 5:48
  • 9
    You can use StandardCharsets.UTF_8 definition instead of plain text. Commented Jul 7, 2015 at 14:34
46

You could use a StringReader and convert the reader to an input stream using the solution in this other stackoverflow post.

22

There are two ways we can convert String to InputStream in Java,

  1. Using ByteArrayInputStream

Example :-

String str = "String contents";
InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
  1. Using Apache Commons IO

Example:-

String str = "String contents"
InputStream is = IOUtils.toInputStream(str, StandardCharsets.UTF_8);
2
  • Hi @anil do you have opinion /experience which one is better? Commented May 8, 2023 at 9:50
  • 1
    my opinion, using Apache commons IO is better option Commented May 8, 2023 at 14:05
1

You can try cactoos for that.

final InputStream input = new InputStreamOf("example");

The object is created with new and not a static method for a reason.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.