Java从InputStream获取String

示例

String可以InputStream使用字节数组构造函数从中读取A。

import java.io.*;

public String readString(InputStream input) throws IOException {
    byte[] bytes = new byte[50]; // 在此处提供字符串的长度(以字节为单位)
    input.read(bytes);
    return new String(bytes);
}

尽管可以指定备用字符集,但它使用系统默认字符集:

return new String(bytes, Charset.forName("UTF-8"));