BufferedInputStream To String Conversion? [duplicate]
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();
Solution 1:
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);
Solution 2:
With Guava:
new String(ByteStreams.toByteArray(inputStream),Charsets.UTF_8);
With Commons / IO:
IOUtils.toString(inputStream, "UTF-8")