BufferedInputStream To String Conversion?

Harinder picture Harinder · Apr 19, 2011 · Viewed 70.4k times · Source

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();

Answer

Nirmal- thInk beYond picture Nirmal- thInk beYond · Apr 19, 2011
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);