我可以从输入流中读取第一行并将其存储到字符串变量中。然后如何读取剩余行并将其复制到另一个输入流以进行进一步处理。
InputStream is1=null;
BufferedReader reader = null;
String todecrypt = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
todecrypt = reader.readLine(); // this will read the first line
String line1=null;
while ((line1 = reader.readLine()) != null){ //loop will run from 2nd line
is1 = new ByteArrayInputStream(line1.getBytes());
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
System.out.println("to decrpt str==="+todecrypt);
然后我将使用 is1 作为第二行的另一个输入流,并将我的示例文件发送到这里
最佳答案
将 Jerry Chin 的评论扩展为完整答案:
你可以这样做
BufferedReader reader = null;
String todecrypt = null;
try {
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
todecrypt = reader.readLine(); // this will read the first line
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
System.out.println("to decrpt str==="+todecrypt);
//This will output the first character of the second line
System.out.println((char)inputStream.read());
您可以将输入流想象成一行字符。读取一个字符就是删除该行中的第一个字符。之后您仍然可以使用 Inputstream 来读取更多字符。 BufferedReader 只是读取 InputStream 直到找到 '\n'。
关于Java 输入流读取两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54803688/