我有一个输入流,其中包含一行字符串,然后是二进制数据。
如果我使用读取该行new BufferedReader(new InputStreamReader(inputStream))
,二进制数据也正在被读取并且不能被重新读取。
如何在不读取二进制数据的情况下读取一行?
*更新:似乎InputStreamReader也缓冲(提前读取):(发布了另一个直接从InputStream读取的答案。
最后手动完成了:(
我想我错过了很多像 \r 和空格这样的情况。
public static String readLine(InputStream inputStream) throws IOException {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
StringBuilder stringBuilder = new StringBuilder();
int c;
for (c = reader.read(); c != '\n' && c != -1 ; c = reader.read()) {
stringBuilder.append((char)c);
}
if (c == -1 && stringBuilder.length() == 0) return null; // End of stream and nothing to return
return stringBuilder.toString();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)