---
title: "How can I read/convert an Input Stream into a String in Java?"  
description: "How can I read/convert an Input Stream into a String in Java?"  
author: "Utpal Vishwas"  
published: 2023-07-21  
updated: 2023-11-17  
canonical: https://www.mindstick.com/forum/159209/how-can-i-read-convert-an-input-stream-into-a-string-in-java  
category: "java"  
tags: ["java", "input validation", "string"]  
reading_time: 2 minutes  

---

# How can I read/convert an Input Stream into a String in Java?

How can I read/[convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) an [Input](https://www.mindstick.com/forum/161440/what-is-first-input-delay-fid) [Stream](https://yourviews.mindstick.com/view/88509/us-intercepts-iranian-tanker-m-t-stream-amid-hormuz-tensions) into a [String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?

## Replies

### Reply by Aryan Kumar

In Java, you can read an **InputStream** and convert it into a **String** using various methods. Here are a couple of common approaches:

### Using Scanner:

This method uses a **Scanner** to read the **InputStream** and set the delimiter to match the entire input. It then checks if there is a next token and returns it as a **String**.

### Using BufferedReader:

javaCopy code

```plaintext
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class InputStreamToString {
    public static String convertToString(InputStream inputStream) throws IOException {
        StringBuilder result = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line).append("\n");
            }
        }
        return result.toString();
    }

    public static void main(String[] args) {
        // Example of usage
        InputStream inputStream = // your input stream here
        try {
            String result = convertToString(inputStream);
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
```

This method uses a **BufferedReader** to read the **InputStream** line by line and append each line to a **StringBuilder**. The resulting **StringBuilder** is then converted to a **String**.

Choose the method that fits your needs and the specific characteristics of your input stream. Keep in mind that the second approach using **BufferedReader** is more suitable for large streams or when processing line-based data.


---

Original Source: https://www.mindstick.com/forum/159209/how-can-i-read-convert-an-input-stream-into-a-string-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
