---
title: Convert inputstream to string
url: https://docs.vultr.com/java/examples/convert-inputstream-to-string
publish_date: 2024-11-21T22:20:00.073501Z
last_updated: 2025-04-10T15:52:06.107149Z
header_image: https://assets.vultrdevrel.com/docs-main-doc-assets-1/collection-items/1000/6476f6e5-d113-426b-8740-59d8c1d71167.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Java Program to Convert InputStream to String

## 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 to convert inputstream to string java effectively.

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  inputstream to string 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.
1. 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.
1. 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.
1. 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 involving input stream to string java conversions.

## 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. These java inputstream to string techniques can be adapted to fit your needs,, ensuring efficient and effective data handling within your Java applications.