Different Ways to Convert InputStream to String java with examples
In this post, We are going to learn how to convert InputStream to String in java with examples.
In Java, if we want to read and write files, We have to use Input and output streams. These I/O Streams are files or devices or any other source.
for example, InputStream
is used to read the data from the file system OutputStream
is used to write the data to the file system
In this example, We are going to learn How to Convert InputStream to String using java io classes, commons-io and guava library, and java8 stream APIs
Apache commons Io library
Apache Commons IO library provides utility classes for Input and Output operations.
Add the commons-io
library into pom.xml
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
Here is an example
- Create a
InputStream
usingFileInputStream
to read the file - Copy the Stream content into Writer using
IOUtils.toString
method
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils.IOUtils;
public class FileTest {
public static void main(String[] args) throws Exception{
InputStream inputStream = new FileInputStream("c:/file.txt");
String myString = IOUtils.toString(inputStream, "UTF-8");
}
}
Using the java.io.StringWriter
class
- Create a
InputStream
usingFileInputStream
to read the file - Create a
StringWriter
to copy string content from file - Copy the Stream content into Writer using
IOUtils.copy
method - Convert to String using
StringWriter.toString()
method
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import org.apache.commons.io.IOUtils.IOUtils;
public class FileTest {
public static void main(String[] args) throws Exception{
InputStream inputStream = new FileInputStream("c:/file.txt");
StringWriter stringWriter = new StringWriter();
IOUtils.copy(inputStream, stringWriter, "UTF-8");
System.out.println(stringWriter.toString();)
}
}
Java8 with BufferedReader
Java8 provides Stream API to read and write files with InputStream and BufferedReader.
- Created an Input Stream for reading file
- Created A
BufferedReader
usingInputStreamReader
class - Call
java8
stream APIlines
method that lazily returns a stream - Finally, Call collect with a line break to convert the input String
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
public class FileTest {
public static void main(String[] args) throws Exception {
InputStream inputStream = new FileInputStream("c:/file.txt");
String output = new BufferedReader(new InputStreamReader(inputStream))
.lines().collect(Collectors.joining("\n"));
}
}
The same above example can be rewritten to read in parallel steam using the parallel()
method
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
public class FileTest {
public static void main(String[] args) throws Exception {
InputStream inputStream = new FileInputStream("c:/file.txt");
String result = new BufferedReader(new InputStreamReader(inputStream))
.lines().parallel().collect(Collectors.joining("\n"));
}
}
guava CharStreams
guava
is a google library to read and write io operations on files.
Add the below dependency to pom.xml
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>
CharStreams.toString
converts input stream into string with specified Charsets.UTF_8
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.google.common.io.CharStreams;
import com.google.common.base.Charsets;
public class FileTest {
public static void main(String[] args) throws Exception {
InputStream inputStream = new FileInputStream("c:/file.txt");
String output = CharStreams.toString(new InputStreamReader(
inputStream, Charsets.UTF_8));
}
}
Conclusion
To summarize, We have learned different ways of reading input files and converting the stream to String in java8,commons-io, and guava library.