我有一个包含字符串和二进制数据的InputStream。
如果我使用new BufferedReader(new InputStreamReader(inputStream))
读取行,那么二进制数据也会被读取并且无法重新读取。
如何在不读取二进制数据的情况下读取一行?
我有一个包含字符串和二进制数据的InputStream。
如果我使用new BufferedReader(new InputStreamReader(inputStream))
读取行,那么二进制数据也会被读取并且无法重新读取。
如何在不读取二进制数据的情况下读取一行?
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();
}
public static String readLine(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int c;
for (c = inputStream.read(); c != '\n' && c != -1 ; c = inputStream.read()) {
byteArrayOutputStream.write(c);
}
if (c == -1 && byteArrayOutputStream.size() == 0) {
return null;
}
String line = byteArrayOutputStream.toString("UTF-8");
return line;
}
DataInputStream
吗?