public class StreamTools {
//构造方法
public static String readStream(InputStream in)throws Exception{
//定义一个内存输出流。
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int len=-1;
byte[] buffer=new byte[1024];
while((len=in.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
in.close();
String content=new String(baos.toByteArray());
return content;
}
}
本文介绍了一个Java工具类StreamTools,该类提供了一个静态方法readStream,用于从输入流中读取数据并将其转换为字符串。通过使用字节数组进行缓冲,此方法能够有效地处理大量数据。
1368

被折叠的 条评论
为什么被折叠?



