JAVA's IO stream has always been a headache for me (I have not learned JAVA in system, so I usually need to implement what functions I need to read the documentation).
Recently I encountered a requirement: read a file byte byte. There are many methods online and a lot of code. I'll share with you a simple method (at least it works for my needs).
File file= new File(fileName); //filename is the file directory, please set InputStream in= null; byte[] bytes= null; in = new FileInputStream(file); //The real thing to use is the read() method of the FileInputStream class bytes= new byte[in.available()]; //in.available() is to get the number of bytes of the file in.read(bytes); //Fill the bytes of the file into the bytes array one by one in.close(); //Remember to close in
Of course, you will need to handle exceptions in the middle. Because everyone has different needs for exception handling, try catch is saved here~~~
It is OK to get the number of bytes of a small file in.available(), but it hasn't tried it in large files. But it should be OK, otherwise what is the significance of this method? ? ?
The above simple example of JAVA reading files by bytes is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.