一次读取一个字节数组
int read(byte[ ] b):读取一个字符
返回值是本次读取到的字节的个数
参数用于每次存储字节数据的数组
读取到文件末尾返回-1
package Test21_Demo.Demo02;/*@create 2020--12--11--10:41*/import java.io.FileInputStream;import java.io.IOException;import java.util.Arrays;public class InputStreamDemo2 {public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("test.txt");/* byte[] bytes = new byte[3];int len = fis.read(bytes);System.out.println(len); // 确定一次性读取到数组的长度是3System.out.println(Arrays.toString(bytes));//[97, 97, 98] - 数组的内容*///循环读取byte[] bytes = new byte[3];int len = -1;while ((len = fis.read(bytes)) != -1) {String s = new String(bytes, 0, len);System.out.println(s);}fis.close();}}
