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 ???