转InputStream为String的几种方法


Ways to convert an InputStream to a String:
  1. Using IOUtils.toString (Apache Utils)
String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
  1. Using CharStreams (guava)
String result = CharStreams.toString(new InputStreamReader( inputStream, Charsets.UTF_8));
  1. Using Scanner (JDK)
Scanner s = new Scanner(inputStream).useDelimiter("\\A");String result = s.hasNext() ? s.next() : "";
  1. Using Stream Api (Java 8). Warning: This solution convert different line breaks (like \r\n) to \n.
String result = new BufferedReader(new InputStreamReader(inputStream)) .lines().collect(Collectors.joining("\n"));
  1. Using parallel Stream Api (Java 8). Warning: This solution convert different line breaks (like \r\n) to \n.
String result = new BufferedReader(new InputStreamReader(inputStream)).lines() .parallel().collect(Collectors.joining("\n"));
  1. Using InputStreamReader and StringBuilder (JDK)
final int bufferSize = 1024;final char[] buffer = new char[bufferSize];final StringBuilder out = new StringBuilder();Reader in = new InputStreamReader(inputStream, "UTF-8");for (; ; ) { int rsz = in.read(buffer, 0, buffer.length); if (rsz < 0) break; out.append(buffer, 0, rsz);}return out.toString();
  1. Using StringWriter and IOUtils.copy (Apache Commons)
StringWriter writer = new StringWriter();IOUtils.copy(inputStream, writer, "UTF-8");return writer.toString();
  1. Using ByteArrayOutputStream and inputStream.read (JDK)
ByteArrayOutputStream result = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int length;while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length);}// StandardCharsets.UTF_8.name() > JDK 7return result.toString("UTF-8");
  1. Using BufferedReader (JDK). Warning: This solution convert different line breaks (like \n\r) to line.separator system property (for example, in Windows to "\r\n").
String newLine = System.getProperty("line.separator");BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));StringBuilder result = new StringBuilder();String line; boolean flag = false;while ((line = reader.readLine()) != null) { result.append(flag? newLine: "").append(line); flag = true;}return result.toString();
  1. Using BufferedInputStream and ByteArrayOutputStream (JDK)
BufferedInputStream bis = new BufferedInputStream(inputStream);ByteArrayOutputStream buf = new ByteArrayOutputStream();int result = bis.read();while(result != -1) { buf.write((byte) result); result = bis.read();}// StandardCharsets.UTF_8.name() > JDK 7return buf.toString("UTF-8");
  1. Using inputStream.read() and StringBuilder (JDK). Warning: This solution has problem with Unicode, for example with Russian text (work correctly only with non-Unicode text)
int ch;StringBuilder sb = new StringBuilder();while((ch = inputStream.read()) != -1) sb.append((char)ch);reset();return sb.toString();
Warning:
  1. Solutions 4, 5 and 9 convert different line breaks to one.
  2. Solution 11 can't work correctly with Unicode text

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值