s-day09--使用字节输入流一次读取一个字节数组

This commit is contained in:
xx
2026-10-23 10:38:48 +08:00
parent 0fd06fdf6f
commit dea4bb06c4
@@ -0,0 +1,42 @@
package com.inmind.outputstream01;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
/*
10.使用字节输入流一次读取一个字节数组
int read(byte[] b) 从字节输入流中将字节数据读取到参数b数组中,返回的值读取的字节个数
需求:使用字节输入流将source1.txt中的数据以一次读取一个字节数组的形式读取打印
*/
public class Demo07 {
public static void main(String[] args) throws IOException {
//1.创建对象
FileInputStream fis = new FileInputStream("source1.txt");
byte[] bytes = new byte[1024];
//2.调用读方法
//一次读取一个字节数组,返回值是读取到的字节个数
//一次读几个字节,就获取几个字节数据
/*int len = fis.read(bytes);
System.out.println(new String(bytes,0,len));
System.out.println(len);
len = fis.read(bytes);
System.out.println(new String(bytes,0,len));
System.out.println(len);
len = fis.read(bytes);
System.out.println(new String(bytes,0,len));
System.out.println(len);*/
int len;//记录读取的字节数量,以及-1结束条件
while ((len = fis.read(bytes))!=-1) {
System.out.println(new String(bytes,0,len));
}
//3.释放资源
fis.close();
}
}