Java Program to Convert InputStream to String

Updated on November 21, 2024
Convert inputstream to string header image

Introduction

When working with Java, a common task you might encounter is converting an InputStream to a String. This occurs frequently in scenarios involving file input/output operations, web data fetching, or reading from resources. The InputStream class is a fundamental part of Java’s IO package and provides a means to read bytes from a source. However, handling these bytes as a string requires some specific steps.

In this article, you will learn how to convert an InputStream to a String using different methods in Java. These will include using standard libraries such as IOUtils from Apache Commons, Java’s own Scanner class, and the newer java.nio package. These methods allow for flexibility depending on your project requirements and the version of Java you are using.

Utilizing the Standard Library

Convert Using Java 8's Standard Charset

  1. Begin by obtaining an InputStream. For example, read from a file or a network resource.

  2. Use java.nio.charset.StandardCharsets and java.util.Scanner to convert the InputStream to a String.

    java
    InputStream inputStream = new FileInputStream("example.txt");
    Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name());
    String text = scanner.useDelimiter("\\A").next();
    scanner.close();
    

    This code creates an InputStream from a file named "example.txt". The Scanner reads the entire content of the stream using a delimiter that matches the beginning of the input, effectively reading the whole stream into a single String. The stream and scanner are then closed to free resources.

Using Apache Commons IO

  1. Add Apache Commons IO to your project dependencies.

  2. Use IOUtils.toString to read the entire InputStream into a String.

    java
    InputStream inputStream = new FileInputStream("example.txt");
    String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
    inputStream.close();
    

    Here, IOUtils.toString method is directly used to convert the InputStream into a String specifying the character encoding explicitly. It's crucial to close the stream afterwards to avoid memory leaks.

Java 11 and Above Approach

Utilizing java.nio.file Files Class

  1. With Java 11, leverage the Files and Path classes for a more modern approach.

  2. Convert the InputStream to a String using the Files.readString after converting the stream to a Path.

    java
    Path path = Paths.get("example.txt");
    String content = Files.readString(path);
    

    This snippet directly reads the contents of a file into a String. This method is straightforward and handles the character encoding automatically, making it suitable for simple file-reading scenarios.

Conclusion

Converting an InputStream into a String in Java can be accomplished through multiple methods, each suitable for different use cases. Whether by utilizing Java's built-in capabilities with Scanner and java.nio.file or using external libraries like Apache Commons IO, you now have the tools to select the appropriate approach based on your specific conditions, such as Java version or project requirements. Adapt these methods to fit your needs, ensuring efficient and effective data handling within your Java applications.