page contents

快速把InputStream转化为String的十种骚操作

我偶尔上Stack Overflow,看了这个200万次阅读量的提问:HowdoI read/convert anInputStreamintoaStringinJava?惊呆了!!!怎么会有这么多人围观。我第一反应的解决办法是使用 Apachecommons包...

我偶尔上Stack Overflow,看了这个200万次阅读量的提问:HowdoI read/convert anInputStreamintoaStringinJava?640?wx_fmt=other

惊呆了!怎么会有这么多人围观。我第一反应的解决办法是使用 Apachecommons包的工具类 IOUtils,果不其然,第一条回答就是这个。640?wx_fmt=other

我的天!居然有2000+的赞!继续往下看,发现大家的不少的骚操作


1 使用CharStreams (Guava)


640?wx_fmt=other


2 使用Scanner

640?wx_fmt=other


3 使用Stream API

640?wx_fmt=other


4 使用parallel Stream API

640?wx_fmt=other


5 使用InputStreamReader and StringBuilder

640?wx_fmt=other


6 使用StringWriter and IOUtils.copy

640?wx_fmt=other


7 使用ByteArrayOutputStream and inputStream.read

640?wx_fmt=other


8 使用BufferedReader

640?wx_fmt=other


9 使用 BufferedInputStream and ByteArrayOutputStream

640?wx_fmt=other


10 使用 inputStream.read() and StringBuilder

640?wx_fmt=other

甚至,还贴出了微基准测试的结果,果然狠人啊。

对于小字符串(length = 175) 结果如下:mode = Average Time, system = Linux, score 1,343 is the best :640?wx_fmt=other

对于大字符串(length = 50100)

结果如下:mode = Average Time, system = Linux, score 200,715 is the best640?wx_fmt=other

如获宝藏,看来需要时不时的逛逛Stack Overflow了。


  • 发表于 2020-03-03 15:47
  • 阅读 ( 1131 )

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
Pack
Pack

1335 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 2172 文章
  3. Pack 1335 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章