Java String to InputStream example shows how to convert String to InputStream in Java. The example also shows how to set character encoding while converting String.
How to convert String to InputStream in Java?
First, convert String to a byte array using the toBytes
method of the String class. Once we get the byte array, use the ByteArrayInputStream class to convert it to the String.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.javacodeexamples.stringexamples; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class StringToInputStreamExample { public static void main(String[] args) throws IOException { String str = "Example String"; /* * Convert string to byte array first. Using the byte * array create ByteArrayInputStream. */ InputStream is = new ByteArrayInputStream( str.getBytes(Charset.forName("UTF-8")) ); /* * Read the InputStream using BufferedReader line * by line. */ BufferedReader br = new BufferedReader( new InputStreamReader(is)); String line = null; while ((line = br.readLine()) != null) System.out.println(line); } } |
Output
1 |
Example String |
It is always advisable to mention the character set while converting the String to a byte array using getBytes
method. If the default character set of the platform you are using is ASCII, and if the String is encoded in UTF-8 encoding, characters will be lost during the conversion as given in the below example.
1 2 3 4 5 6 7 8 |
String str = "Ã string çöntäining nön äsçii çhäräçters"; InputStream is = new ByteArrayInputStream( str.getBytes()); BufferedReader br = new BufferedReader( new InputStreamReader(is)); String line = null; while ((line = br.readLine()) != null) System.out.println(line); |
Output
1 |
? string ??nt?ining n?n ?s?ii ?h?r??ters |
As you can see from the output, the missing characters are displayed as question marks.
Convert String to InputStream using the Apache Commons library
If you are using the Apache Commons library, you can use the toInputStream
method of IOUtils class to convert the String.
1 |
public static InputStream toInputStream(CharSequence inputString, String encoding) |
This method converts the specified String to InputStream with bytes encoded using a specified encoding.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.javacodeexamples.stringexamples; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.io.IOUtils; public class StringToInputStreamExample { public static void main(String[] args) throws IOException { String str = "Example String"; /* * You can use toInputStream method of Apache * Commons IOUtils class to convert String. */ InputStream is = IOUtils.toInputStream(str, "UTF-8"); BufferedReader br = new BufferedReader( new InputStreamReader(is)); String line = null; while ((line = br.readLine()) != null) System.out.println(line); } } |
Output
1 |
Example String |
This example is a part of the Java String tutorial.
Please let me know your views in the comments section below.