Java reads a simple instance of a file by byte _java

Source: Internet
Author: User

The Java IO Stream has always been a headache for me (I didn't have a system to learn Java, what I would normally need to do to see the document).

Recently encountered a requirement: read a file byte by byte. A lot of methods on the Web, code a whole bunch. I'm here to share with you a simple approach (at least for my needs to be effective).

File file= new file (fileName);  FileName is the file directory, please set
inputstream in= null;
Byte[] bytes= null;

in = new FileInputStream (file);  What really needs to be used is the read () method of the FileInputStream class
bytes= new Byte[in.available ()];  In.available () is the number of bytes
in.read (bytes) that gets the file;  Fill the file byte one by one into the bytes array
in.close ();  Remember to close in

Of course, you'll need to handle the anomalies in the middle. Since everyone has different needs for handling exceptions, try Catch is omitted here ~ ~ ~

In.available () The number of bytes to get small files is OK, but large files have not been tried. But it should be OK, otherwise the meaning of this method???

The above Java reading file by byte is a simple example of the small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.