Read (char[] b,int off,int len) and read (char[] b) Differences

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.