Java如何将InputStream转换为字符串?

Talk is cheap, Show me the code. -- by: Linus Torvalds
方式一、
JAVA8+ Stream API
String result = new BufferedReader(new InputStreamReader(inputStream))  .lines() // 按行读取  .collect(Collectors.joining("n")); // 合并行为字符串方式二、
使用commons-io
【Java如何将InputStream转换为字符串?】String result = IOUtils.toString(inputStream, "UTF-8");方式三、
使用Guava
String result = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8"));方式四、
使用ByteArrayOutputStream
BufferedInputStream bis = new BufferedInputStream(inputStream);ByteArrayOutputStream buf = new ByteArrayOutputStream();int result = bis.read();while(result != -1) {    buf.write((byte) result);    result = bis.read();}return buf.toString("UTF-8");



    推荐阅读