Java中InputStream流的多次读取的实现示例
更新时间:2026年01月26日 09:11:12 作者:一线大码
本文主要介绍了Java中InputStream流的多次读取的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
在读取 InputStream 的时候,其实内部是有个指针,它指示每次读取之后下一次要读取的起始位置,当第一次读完的时候,指针已经指到最后了,当再次读取的时候,自然是读不到数据的。
1. 使用 StringBuilder 中转
public static void main(String[] args) throws IOException {
String filePath = "C:\\Users\\wangb\\Desktop\\test.txt";
// 使用Files.newInputStream方法获取InputStream
InputStream is = Files.newInputStream(Paths.get(filePath));
// builder 保留读到的数据
StringBuilder builder = new StringBuilder();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
builder.append(new String(buffer, 0, len));
}
System.out.println("-------第一次需要InputStream,那么就创建一个-------");
InputStream isOne = new ByteArrayInputStream(builder.toString().getBytes());
// 下面是测试新的InputStream到底是否有数据
byte[] bufferOne = new byte[1024];
int lenOne;
while ((lenOne = isOne.read(bufferOne)) != -1) {
System.out.println(new String(bufferOne, 0, lenOne));
}
System.out.println("-------第二次需要InputStream,那么就再创建一个-------");
InputStream isTwo = new ByteArrayInputStream(builder.toString().getBytes());
// 下面是测试新的InputStream到底是否有数据
byte[] bufferTwo = new byte[1024];
int lenTwo;
while ((lenTwo = isTwo.read(bufferTwo)) != -1) {
System.out.println(new String(bufferTwo, 0, lenTwo));
}
is.close();
isOne.close();
isTwo.close();
}
2. 使用 ByteArrayOutputStream 中转
public static void main(String[] args) throws IOException {
String filePath = "C:\\Users\\wangb\\Desktop\\test.txt";
// 使用Files.newInputStream方法获取InputStream
InputStream is = Files.newInputStream(Paths.get(filePath));
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
byteArrayOut.write(buffer, 0, len);
}
System.out.println("-------第一次需要InputStream,那么就创建一个-------");
InputStream isOne = new ByteArrayInputStream(byteArrayOut.toByteArray());
// 下面是测试新的InputStream到底是否有数据
byte[] bufferOne = new byte[1024];
int lenOne;
while ((lenOne = isOne.read(bufferOne)) != -1) {
System.out.println(new String(bufferOne, 0, lenOne));
}
System.out.println("-------第二次需要InputStream,那么就再创建一个-------");
InputStream isTwo = new ByteArrayInputStream(byteArrayOut.toByteArray());
// 下面是测试新的InputStream到底是否有数据
byte[] bufferTwo = new byte[1024];
int lenTwo;
while ((lenTwo = isTwo.read(bufferTwo)) != -1) {
System.out.println(new String(bufferTwo, 0, lenTwo));
}
is.close();
isOne.close();
isTwo.close();
}
3. 使用 ByteArrayInputStream 重置指针
public static void main(String[] args) throws IOException {
String filePath = "C:\\Users\\wangb\\Desktop\\test.txt";
// 使用Files.newInputStream方法获取InputStream
InputStream is = Files.newInputStream(Paths.get(filePath));
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
byteArrayOut.write(buffer, 0, len);
}
// 创建一个新的 ByteArrayInputStream 对象,包含复制的数据
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOut.toByteArray());
System.out.println("-------第一次使用-------");
// 下面是测试新的InputStream到底是否有数据
byte[] bufferOne = new byte[1024];
int lenOne;
while ((lenOne = byteArrayInputStream.read(bufferOne)) != -1) {
System.out.println(new String(bufferOne, 0, lenOne));
}
// 重置 ByteArrayInputStream 的指针到开头
byteArrayInputStream.reset();
System.out.println("-------第二次使用-------");
// 下面是测试新的InputStream到底是否有数据
byte[] bufferTwo = new byte[1024];
int lenTwo;
while ((lenTwo = byteArrayInputStream.read(bufferTwo)) != -1) {
System.out.println(new String(bufferTwo, 0, lenTwo));
}
is.close();
byteArrayInputStream.close();
}
到此这篇关于Java中InputStream流的多次读取的实现示例的文章就介绍到这了,更多相关Java InputStream流读取内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
相关文章
一文详解Java中三大异常处理方式(try-catch、throw、throws)
这篇文章主要介绍了Java中三大异常处理方式的相关资料,分别是try-catch、throw、throws,try-catch、throw、throws,try-catch用于捕获和处理异常,throw用于显式抛出异常,throws用于声明方法可能抛出的异常,需要的朋友可以参考下2026-01-01
BiConsumer接口中的方法andThen accept使用详解
这篇文章主要为大家介绍了BiConsumer接口中的方法andThen accept使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-07-07


最新评论