today, when reading hexadecimal data from a file, read in Java's read (char[] b) method, and the result is that the data printed is inconsistent with the end of the data in the file. Then there is a read (char[] b,int off,int len) method. I went to check out one of their differences, just read (char[] b,int off,int len) can solve my problem. Below is the online reference information, very easy to understand.
This is the source of the read (char cbuf[]) method, it can be seen that the Read method is actually called the read (char[] b,int off,int len) This method, but is to set Len to the length of the array
public int read (char cbuf[]) throws IOException {return read (cbuf, 0, cbuf.length);}
Take a look at Read (char[] b,int off,int len) This method, the source is a bit complicated, not to read, probably mean to say: Read the character into a part of the array, Len is to read the maximum number of characters, of course, can be smaller than him.
Now for example, for example, a file has 1024 + 40 bytes, and read (char cbuf[]) first reads 1024 bytes into the array, and the second one must read 1024 bytes, because the number
The team leader is 1024, but he has only 40 bytes. This time the first 40 bytes are overwritten, and the subsequent bytes are in the last array.
and read (char[] b,int off,int len) This method is not the same, the first time he read 1024 bytes, the second time he saw enough, read 40 bytes into the array, the remaining
The space is empty.
So when we read and write files, we try to use the following code (if binary is a byte array)
Char[] Buff=new char[1024];int i=0;while ((I=reader.read (buff,0,1024))!=-1) {System.out.println (i);p w.write (buff,0, i);}
Read (char[] b,int off,int len) and read (char[] b) Differences