且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取一个输出流到一个字符串中

更新时间:2023-02-05 07:40:00

我会使用 ByteArrayOutputStream.完成后您可以调用:

I would use a ByteArrayOutputStream. And on finish you can call:

new String( baos.toByteArray(), codepage );

或更好:

baos.toString( codepage );

对于 String 构造函数,codepage 可以是 Stringjava.nio.charset.Charset.一个可能的值是 java.nio.charset.StandardCharsets.UTF_8.

For the String constructor, the codepage can be a String or an instance of java.nio.charset.Charset. A possible value is java.nio.charset.StandardCharsets.UTF_8.

方法 toString() 只接受一个 String 作为 codepage 参数(支持 Java 8).

The method toString() accepts only a String as a codepage parameter (stand Java 8).