缓冲输入流到字符串的转换?

2022-08-31 13:51:04

可能的重复:
在Java中,如何将输入流读取/转换为字符串?

嗨,我想将此缓冲输入流转换为我的字符串。我该怎么做?

BufferedInputStream in = new BufferedInputStream(sktClient.getInputStream() );
String a= in.read();

答案 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);

答案 2

番石榴

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

使用共享资源/ IO

IOUtils.toString(inputStream, "UTF-8")