Hi,
I have an input stream in Java and convert it to a String. what is the best code for for this?
How to Read/Convert an inputStream to a String?
My object is of java.io.InputStream class.
Thanks
Hi,
The best method is to use the org.apache.commons.io.IOUtils class which provides methods for converting stream into a String.
Add Maven dependency in your pom.xml:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency>
Now you can use the commons-io library in code. Here is the full code of Reading /Converting an inputStream to a String.
package net.roseindia; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import org.apache.commons.io.IOUtils; public class InputStreamToString { public static void main(String[] args) { //Construct simple input stream //Input stream can be from file or other source also //and it will work String str = "Learn Java at http://www.roseindia.net"; //Convert String into InputStream InputStream inputStream = new ByteArrayInputStream(str.getBytes()); StringWriter writer = new StringWriter(); try { IOUtils.copy(inputStream, writer, "UTF-8"); } catch (IOException e) { e.printStackTrace(); } String theString = writer.toString(); System.out.println("String Value: "+theString); } }
First create StringWriter:
StringWriter writer = new StringWriter();
Then copy the stream into writer:
IOUtils.copy(inputStream, writer, "UTF-8");
Get String from writer:
String theString = writer.toString();
Thanks
Hi,
You can also Read/Convert an inputStream to a String with Scanner class. Here is the full code for the conversion:
package net.roseindia; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.util.Scanner; import org.apache.commons.io.IOUtils; public class InputStreamToStringWithScanner { public static void main(String[] args) { //Construct simple input stream //Input stream can be from file or other source also //and it will work String str = "Learn Java at http://www.roseindia.net"; //Convert String into InputStream InputStream inputStream = new ByteArrayInputStream(str.getBytes()); String inputStreamString = new Scanner(inputStream,"UTF-8").useDelimiter("\\A").next(); System.out.println(inputStreamString); } }
Thanks
Hi,
In Pure Java you can use following classes:
StringBuilder
BufferedReader
classes for converting stream into String.
Here is full source code that you may try:
package net.roseindia; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.util.Scanner; import org.apache.commons.io.IOUtils; public class InputStreamToStringWithScanner { public static void main(String[] args) { //Construct input stream String str = "Learn Java at http://www.roseindia.net"; InputStream inputStream = new ByteArrayInputStream(str.getBytes()); StringBuilder inputStringBuilder = new StringBuilder(); BufferedReader bufferedReader; try { bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); String line = bufferedReader.readLine(); while (line != null) { inputStringBuilder.append(line); inputStringBuilder.append('\n'); line = bufferedReader.readLine(); } System.out.println(inputStringBuilder.toString()); } catch (Exception e) { e.printStackTrace(); } } }
Thanks
Ads