Guava equivalent for IOUtils.toString(InputStream)

前端 未结 9 1088
深忆病人
深忆病人 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条回答
  •  一整个雨季
    2020-12-04 16:44

    UPDATE: Looking back, I don't like my old solution. Besides it is 2013 now and there are better alternatives available now for Java7. So here is what I use now:

    InputStream fis = ...;
    String text;
    try (  InputStreamReader reader = new InputStreamReader(fis, Charsets.UTF_8)){
            text = CharStreams.toString(reader);
    }
    

    or if with InputSupplier

    InputSupplier spl = ...
    try (  InputStreamReader reader = spl.getInput()){
            text = CharStreams.toString(reader);
        }
    

提交回复
热议问题