Introduction: Why This Question Haunts Every Second Java Developer
\n\nIf 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?"
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\nIn 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\nLet's go! 🚀
\n\nPreparation: What You Need to Know Before Reading an InputStream
\n\nBefore we dive into the code, let's agree on two things:
\n\n- \n
- Encoding — always explicitly specify the encoding (
UTF-8is 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
For all examples, we will use this simple test stream:
\n\nInputStream inputStream = new ByteArrayInputStream(\n "Hello, Java world!".getBytes(StandardCharsets.UTF_8)\n);\n\nMethod 1: Classic — BufferedReader (Java 7+)
\n\nThe oldest and most proven method, which still works great. Ideal when you want to read line by line or control buffering.
\n\npublic 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\nPros: Transparent code, easy to read and debug. Works with Java 7+.
\nCons: Adds an extra newline character at the end. Not the most performant.
\n\nMethod 2: Scanner — When You Need More Flexibility (Java 5+)
\n\nMany forget that Scanner can read not only from the console. It's a great tool for data parsing.
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\nThe magic here is in useDelimiter("\\\\A"). The regex \\A means "beginning of input", so Scanner reads the entire stream as one token.
Pros: Very concise, flexible delimiter settings.
\nCons: Scanner is a heavy class for simple reading. There's a lot of unnecessary logic under the hood.
\n\nMethod 3: Java 8 Streams — Modern and Elegant
\n\nWith the introduction of Stream API in Java 8, we got an elegant way to process data. For InputStream, it looks like this:
\n\npublic 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}