72

Possible Duplicate:
In Java how do a read/convert an InputStream in to a string?

Hi, I want to convert this BufferedInputStream into my string. How can I do this?

BufferedInputStream in = new BufferedInputStream(sktClient.getInputStream() );
String a= in.read();
Yash Joshi
  • 557
  • 7
  • 25
Harinder
  • 11,776
  • 16
  • 70
  • 126

5 Answers5

51
BufferedInputStream in = new BufferedInputStream(sktClient.getInputStream());
byte[] contents = new byte[1024];

int bytesRead = 0;
String strFileContents; 
while((bytesRead = in.read(contents)) != -1) { 
    strFileContents += new String(contents, 0, bytesRead);              
}

System.out.print(strFileContents);
Community
  • 1
  • 1
Nirmal- thInk beYond
  • 11,847
  • 8
  • 35
  • 46
  • 5
    one small bug. in the while loop you should be appending with each iteration. it should be += instead of =. ie: strFileContents += new String(contents, 0, bytesRead); – JJ_Coder4Hire Jun 26 '14 at 20:54
  • 3
    @JJ_Coder4Hire that isnt the only bug, this code relies on chance that the string encoding has a boundary at the bytesRead mark (which is an ok assumption **ONLY** for ASCII). – chacham15 Jul 15 '14 at 07:56
  • I had to put 'System.out.print(strFileContents);' inside loop else only last piece of my html response was shown. btw thanks – WoutVanAertTheBest Jan 14 '15 at 14:54
37

With Guava:

new String(ByteStreams.toByteArray(inputStream),Charsets.UTF_8);

With Commons / IO:

IOUtils.toString(inputStream, "UTF-8")
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
19

I suggest you use apache commons IOUtils

String text = IOUtils.toString(sktClient.getInputStream());
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
11

Please following code

Let me know the results

public String convertStreamToString(InputStream is)
                throws IOException {
            /*
             * To convert the InputStream to String we use the
             * Reader.read(char[] buffer) method. We iterate until the
    35.         * Reader return -1 which means there's no more data to
    36.         * read. We use the StringWriter class to produce the string.
    37.         */
            if (is != null) {
                Writer writer = new StringWriter();

                char[] buffer = new char[1024];
                try
                {
                    Reader reader = new BufferedReader(
                            new InputStreamReader(is, "UTF-8"));
                    int n;
                    while ((n = reader.read(buffer)) != -1) 
                    {
                        writer.write(buffer, 0, n);
                    }
                }
                finally 
                {
                    is.close();
                }
                return writer.toString();
            } else {       
                return "";
            }
        }

Thanks, Kariyachan

Dev.Sinto
  • 6,802
  • 8
  • 36
  • 54
6

If you don't want to write it all by yourself (and you shouldn't really) - use a library that does that for you.

Apache commons-io does just that.

Use IOUtils.toString(InputStream), or IOUtils.readLines(InputStream) if you want finer control.

Eran Harel
  • 2,325
  • 19
  • 28