How to Read InputStream to String in Java: Best Practices 2025

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why This Question Haunts Every Second Java Developer

\n\n

If you've ever written Java code that reads data from a network, file, or any other I/O system, you've certainly encountered the InputStream class. And almost immediately a natural question arises: "How do I read this InputStream and turn it into a regular string?"

\n\n

On Stack Overflow, this question has gathered over 2.5 million views and hundreds of answers. Why? Because a seemingly simple task hides many pitfalls: encodings, performance, memory leaks, compatibility with different Java versions.

\n\n

In this article, we will cover 5 best ways to convert InputStream to String in Java, relevant for 2025. You will learn not only "how" but also "why" you should choose one method over another. And at the end, you'll get a checklist for quick decision-making in real projects.

\n\n

Let's go! 🚀

\n\n

Preparation: What You Need to Know Before Reading an InputStream

\n\n

Before we dive into the code, let's agree on two things:

\n\n
    \n
  • Encoding — always explicitly specify the encoding (UTF-8 is the gold standard). Never rely on the system's default encoding.
  • \n
  • Closing resources — use try-with-resources (Java 7+) to ensure the stream is closed.
  • \n
\n\n

For all examples, we will use this simple test stream:

\n\n
InputStream inputStream = new ByteArrayInputStream(\n    "Hello, Java world!".getBytes(StandardCharsets.UTF_8)\n);
\n\n

Method 1: Classic — BufferedReader (Java 7+)

\n\n

The oldest and most proven method, which still works great. Ideal when you want to read line by line or control buffering.

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

Pros: Transparent code, easy to read and debug. Works with Java 7+.

\n

Cons: Adds an extra newline character at the end. Not the most performant.

\n\n

Method 2: Scanner — When You Need More Flexibility (Java 5+)

\n\n

Many forget that Scanner can read not only from the console. It's a great tool for data parsing.

\n\n
public static String convertUsingScanner(InputStream inputStream) {\n    try (Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) {\n        scanner.useDelimiter("\\\\A"); // \\\\A — beginning of file, read everything at once\n        return scanner.hasNext() ? scanner.next() : "";\n    }\n}
\n\n

The magic here is in useDelimiter("\\\\A"). The regex \\A means "beginning of input", so Scanner reads the entire stream as one token.

\n\n

Pros: Very concise, flexible delimiter settings.

\n

Cons: Scanner is a heavy class for simple reading. There's a lot of unnecessary logic under the hood.

\n\n

Method 3: Java 8 Streams — Modern and Elegant

\n\n

With the introduction of Stream API in Java 8, we got an elegant way to process data. For InputStream, it looks like this:

\n\n
public static String convertUsingStreams(InputStream inputStream) throws IOException {\n    try (BufferedReader reader = new BufferedReader(\n            new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {\n        return reader.lines()\n                .collect(Collectors.joining("\\n"));\n    }\n}

Blogs

Book Recommendations