---
title: "Read/convert an InputStream to a String"  
description: "Read/convert an InputStream to a String"  
author: "Anonymous User"  
published: 2015-05-04  
updated: 2015-05-04  
canonical: https://www.mindstick.com/forum/23181/read-convert-an-inputstream-to-a-string  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Read/convert an InputStream to a String

If you have java.io.[InputStream](https://www.mindstick.com/interview/2459/what-is-the-difference-between-the-reader-writer-class-hierarchy-and-the-inputstream-outputstream-class-hierarchy) object, how should you [process](https://yourviews.mindstick.com/story/1525/7-important-factors-that-may-affect-the-learning-process) that object and [produce](https://yourviews.mindstick.com/view/83488/how-much-power-do-solar-panels-produce) a [String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp)?\
Suppose I have an InputStream that contains [text data](https://answers.mindstick.com/qa/112298/how-do-i-implement-sentiment-analysis-in-text-data), and I want to [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) this to a String (for example, so I can write the contents of the [stream](https://yourviews.mindstick.com/view/88509/us-intercepts-iranian-tanker-m-t-stream-amid-hormuz-tensions) to a [log file](https://www.mindstick.com/articles/327201/create-log-file-in-c-sharp)).\
What is the easiest way to take the InputStream and convert it to a String?\

```
public String convertStreamToString(InputStream is) {     // ???}
```

## Replies

### Reply by Anonymous User

A nice way to do this is using Apache commons IOUtils to copy the InputStream into a StringWriter... something like\

```
StringWriter writer = new StringWriter();IOUtils.copy(inputStream, writer, encoding);String theString = writer.toString();
```

or even\
// NB: does not close inputStream, you can use IOUtils.closeQuietly for that

```
String theString = IOUtils.toString(inputStream, encoding); 
```

Alternatively, you could use ByteArrayOutputStream if you don't want to mix your Streams and Writers \
Taking into account [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) one should first get a java.io.Reader instance. This can then be read and added to a StringBuilder (we don't need StringBuffer if we are not accessing it in multiple threads, and StringBuilder is faster). The trick here is that we work in blocks, and as such don't need other buffering streams. The block size is parameterized for run-time performance optimization.\

```
public static String slurp(final InputStream is, final int bufferSize){  final char[] buffer = new char[bufferSize];  final StringBuilder out = new StringBuilder();  try (Reader in = new InputStreamReader(is, "UTF-8")) {    for (;;) {      int rsz = in.read(buffer, 0, buffer.length);      if (rsz < 0)        break;      out.append(buffer, 0, rsz);    }  }  catch (UnsupportedEncodingException ex) {    /* ... */  }  catch (IOException ex) {      /* ... */  }  return out.toString();}
```

\
Also,

```
import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.io.IOException;    public static String readInputStreamAsString(InputStream in)     throws IOException {    BufferedInputStream bis = new BufferedInputStream(in);    ByteArrayOutputStream buf = new ByteArrayOutputStream();    int result = bis.read();    while(result != -1) {      byte b = (byte)result;      buf.write(b);      result = bis.read();    }            return buf.toString();}
```


---

Original Source: https://www.mindstick.com/forum/23181/read-convert-an-inputstream-to-a-string

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
