Java InputStream to String

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

This tutorial delves into converting an InputStream to a String in Java, leveraging the versatile I/O Streams for data reading and writing. Focusing on the FileInputStream class, commonly used for raw byte data like images, we explore plain Java methods, incorporating Java 8/9 features. Additionally, alternatives using Guava and Apache Commons IO libraries are discussed for comprehensive solutions.

Converting Java InputStream to String with Java – StringBuilder

Let's examine a straightforward, lower-level strategy utilising standard Java, an InputStream, and a straightforward StringBuilder:

Converting Java InputStream to String with Java 8 – BufferedReader

The BufferedReader now has a new lines() function, thanks to Java 8. Let's look at how to utilise it to transform an InputStream into a String.

Converting Java InputStream to String with Java 9 – InputStream.readAllBytes()

We can use a new readAllBytes method that was added to the InputStream if we're running Java 9 or higher.

We must be aware that this straightforward method is designed for straightforward scenarios in which reading all bytes into a byte array is practical. For reading input streams with a lot of data, we shouldn't use it.

Converting Java InputStream to String Using InputStreamReader Class

The read() method of the InputStreamReader class accepts a character array as a parameter and reads the contents of the current Stream to the given array. To convert an InputStream Object int to a String using this method.

Converting Java InputStream to String Using the Scanner Class

Although not very common, using the Scanner class does work. It works because when Scanner loops through the stream's tokens, we can separate them using the "beginning of the input boundary," which gives us a single token for the stream's complete contents.

Converting Java InputStream to String Using ByteArrayOutputStream

With minimal conversion, support for input streams with vast amounts of data, preservation of the original line breaks, and compatibility with all Java versions, this ByteArrayOutputStream solution is the quickest way to convert an InputStream to a String.

Converting Java InputStream to String with java.nio

Another option is to save the InputStream's content to a file before converting it to a String.

We're using the java.nio.file in this case. To create a temporary file and copy the content of the input stream to the file, use the Files class. The readAllBytes() method of the same class is then used to transform the contents of the file into a String.

Converting Java InputStream to String with Guava

Guava library contains a number of excellent classes and methods for carrying out IO tasks. These classes cover any otherwise exposed complications.

  • Using ByteSource A readable source of bytes, such as a file, is represented by the term "ByteSource". It has utility methods, which are frequently used by opening a stream, performing an action, and then closing the opened stream. Bytes read from a source are decoded as characters in the specified Charset by the asCharSource(charset) method. As a result of the method, it returns the characters as a String.
  • Using CharStreams Additionally, utility methods for working with character streams are provided by the CharStreams class. An InputStream can be transformed into a String by using InputStreamReader and CharStreams.

    A Java programme that uses the CharStreams class in the Google Guava package converts an input stream to a string.

Converting Java InputStream to String with Apache Commons IO

  • Dependencies Include the following dependencies in the project to include common-io jars.
  • Using IOUtils IOUtils, a class in Apache Commons, is incredibly helpful for reading file content into a string. It greatly simplifies and makes the code easier to read. Additionally, it offers improved performance.

    Use either of the two methods-

    • IOUtils.copy()
    • IOUtils.toString()

Conclusion

We've looked at turning an InputStream into a String in Java in this tutorial.

  • StringBuilder or Java InputStream with BufferedReader class can be used for lower-level strategy utilizing standard Java.
  • InputStream.readAllBytes() can be used from Java 9 onwards for a simple implementation.
  • Scanner class although not very common can be used to achieve the same functionality.
  • We can also use java.nio.file to save the InputStream's content to a file before converting it to a String.
  • Apache IO or Guava External Library can also be used for the same purpose. These libraries extremely simplify the I/O operations.