Guava equivalent for IOUtils.toString(InputStream)

前端 未结 9 1081
深忆病人
深忆病人 2020-12-04 16:00

Apache Commons IO has a nice convenience method IOUtils.toString() to read an InputStream to a String.

Since I am trying to move away from Apache Common

9条回答
  •  -上瘾入骨i
    2020-12-04 16:49

    Another option is to read bytes from Stream and create a String from them:

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

    It's not 'pure' Guava, but it's a little bit shorter.

提交回复
热议问题