JAVA reads files by byte and JAVA reads files by byte

Source: Internet
Author: User

JAVA reads files by byte and JAVA reads files by byte

Java io streams have always been a headache for me (I have never systematically studied JAVA, and I usually need to implement some functions before reading the document ).

Recently, I encountered a requirement that one byte reads one file in one byte. There are many methods and Code on the Internet. I am here to share with you a simple method (at least it is effective for my needs ).

1 File file = new File (fileName); // filename is the File directory. Set 2 InputStream in = null; 3 byte [] bytes = null; 4 5 in = new FileInputStream (file); // The read () method 6 bytes = new byte [in. available ()]; // in. available () is the number of bytes of the obtained file 7 in. read (bytes); // fill in the bytes of the file one by one in the bytes array. close (); // remember to close in

Of course, you will need to handle exceptions in the middle. Because each person has different requirements for exception handling, try catch is omitted here ~~~

In. available () can be used to obtain the number of bytes of a small file, but the large file has not been tried. But it should also work. Otherwise, what is the significance of this method ???

 

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.