The read method of FileInputStream
explain:
int read() :
(1) Read a byte of data and return (return in int type)
(2) Call again to read the next unread byte
(3) Return -1 at the end of the file (no real data has been read)
Demo file
code
FileInputStream fileInputStream = new FileInputStream(new File("E:\\aa.txt"));
int read1 = fileInputStream.read();
int read2 = fileInputStream.read();
int read3 = fileInputStream.read();
System.out.println(read1);
System.out.println(read2);
System.out.println(read3);
output
int read(byte[] b) :
(1) The read data is stored in the b array, and b.length bytes are read at a time
(2) The actual length of the read data is returned (it is possible that the length of the b array is 100, but the actual data is only 5, then return 5 )
(3) Call again to read from the next unread byte
(4) Return -1 at the end of the file (no real data has been read)
demo file
code
FileInputStream fileInputStream = new FileInputStream(new File("E:\\aa.txt"));
byte[] bytes1=new byte[5];
int read1 = fileInputStream.read(bytes1);
System.out.println(read1);
System.out.println(Arrays.toString(bytes1));
byte[] bytes2=new byte[5];
int read2 = fileInputStream.read(bytes2);
System.out.println(read2);
System.out.println(Arrays.toString(bytes2));
byte[] bytes3=new byte[5];
int read3 = fileInputStream.read(bytes3);
System.out.println(read3);
System.out.println(Arrays.toString(bytes3));
output
int read(byte[] b, int off, int len) :
(1) Store the read data into b
(2) Start storing from the array subscript off (including), read len bytes (len<=b.length-off)
(3) Return the read data Real length
(4) call again to read from the next unread byte
(5) return -1 at the end of the file (no real data is read)
demo file
the code
FileInputStream fileInputStream = new FileInputStream(new File("E:\\aa.txt"));
byte[] bytes1=new byte[5];
int read1 = fileInputStream.read(bytes1, 2, 3);
System.out.println(read1);
System.out.println(Arrays.toString(bytes1));
byte[] bytes2=new byte[5];
int read2 = fileInputStream.read(bytes2, 2, 3);
System.out.println(read2);
System.out.println(Arrays.toString(bytes2));
byte[] bytes3=new byte[5];
int read3 = fileInputStream.read(bytes2, 2, 3);
System.out.println(read3);
System.out.println(Arrays.toString(bytes3));
output