好吧,首先您需要区分 字节 和 字符
。您可以一次从
InputStream一定数量的字节中读取(作为最大数量;不能保证您将获得所需的所有字节),并且一次可以读取
Reader多个字符(再次,最大)。
听起来您 可能
想要在
InputStreamReader周围使用
InputStream,指定适当的字符编码,然后从中读取
InputStreamReader。如果必须有
准确 的字符数,则需要循环-例如:
public static String readExactly(Reader reader, int length) throws IOException { char[] chars = new char[length]; int offset = 0; while (offset < length) { int charsRead = reader.read(chars, offset, length - offset); if (charsRead <= 0) { throw new IOException("Stream terminated early"); } offset += charsRead; } return new String(chars);}


