package com.inmind.io_in02; import java.io.FileInputStream; import java.io.IOException; /* 使用字节输入流一次读取一个字节数组 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"); /* 2.调用读方法(一次读取一个字节数组的数据) int read(byte[] b) 从字节输入流中将字节数据读取到参数b数组中,返回的值读取的字节个数 */ /*byte[] bytes = new byte[2];//用来保存,字节输入流读取到的字节数据 int len = fis.read(bytes);//用来接收读取到的字节数据的个数 System.out.println(len); System.out.println(new String(bytes,0,len)); len = fis.read(bytes); System.out.println(len); System.out.println(new String(bytes,0,len)); len = fis.read(bytes); System.out.println(len); System.out.println(new String(bytes,0,len)); len = fis.read(bytes); System.out.println(len); System.out.println(new String(bytes,0,len));*/ byte[] bytes = new byte[1024];//用来保存字节输入流读取到的字节数据 int len;//用来接收读取到的字节数据的个数,如果返回-1表示读取到末尾了 while ((len = fis.read(bytes)) != -1) {//(核心代码) System.out.println(new String(bytes,0,len)); } //3.释放资源 fis.close(); System.out.println("程序结束"); } }