Given a string:
String exampleString = "example";
How do I convert it to an InputStream
?
-
2InputStreams look at bytes, Readers look at characters.Thorbjørn Ravn Andersen– Thorbjørn Ravn Andersen2021年03月28日 18:11:23 +00:00Commented Mar 28, 2021 at 18:11
5 Answers 5
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"
.
-
2Doesn'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?Jonathan Ramos– Jonathan Ramos2013年09月18日 11:28:20 +00:00Commented Sep 18, 2013 at 11:28
-
22StandardCharsets requires minimum API level 19.Nantoka– Nantoka2014年01月30日 15:05:43 +00:00Commented 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.Cruncher– Cruncher2014年03月12日 18:28:35 +00:00Commented Mar 12, 2014 at 18:28
-
27@Nantoka Instead of StandardCharsets.UTF_8 you can use Charset.forName("UTF-8") for any API Level.PJ_Finnegan– PJ_Finnegan2014年12月24日 09:19:21 +00:00Commented Dec 24, 2014 at 9:19
-
1Not pretty if you have large string objects and want to save on memory/allocations. A proper streaming solution would be nice.Dave Moten– Dave Moten2023年01月31日 23:25:04 +00:00Commented Jan 31, 2023 at 23:25
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.
-
7They used new ByteArrayInputStream(exampleString.getBytes("UTF-8")). So it will be optimized way to use InputStream stream = new ByteArrayInputStream(exampleString.getBytes("UTF-8"));Pankaj Kumar– Pankaj Kumar2011年08月24日 12:34:18 +00:00Commented Aug 24, 2011 at 12:34
-
11@PankajKumar: Java's JIT compiler is more than able to inline this.Andrew White– Andrew White2012年07月20日 12:14:54 +00:00Commented Jul 20, 2012 at 12:14
-
9Using a method which doesn't specify encoding is a terrible idea...arkon– arkon2013年06月05日 02:01:32 +00:00Commented 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");Cuga– Cuga2014年01月11日 05:48:49 +00:00Commented Jan 11, 2014 at 5:48
-
9You can use
StandardCharsets.UTF_8
definition instead of plain text.douglaslps– douglaslps2015年07月07日 14:34:33 +00:00Commented Jul 7, 2015 at 14:34
You could use a StringReader and convert the reader to an input stream using the solution in this other stackoverflow post.
There are two ways we can convert String to InputStream in Java,
- Using ByteArrayInputStream
Example :-
String str = "String contents";
InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
- Using Apache Commons IO
Example:-
String str = "String contents"
InputStream is = IOUtils.toInputStream(str, StandardCharsets.UTF_8);
-
Hi @anil do you have opinion /experience which one is better?soMuchToLearnAndShare– soMuchToLearnAndShare2023年05月08日 09:50:01 +00:00Commented May 8, 2023 at 9:50
-
1my opinion, using Apache commons IO is better optionAnil Nivargi– Anil Nivargi2023年05月08日 14:05:34 +00:00Commented May 8, 2023 at 14:05
Explore related questions
See similar questions with these tags.