Breaking News
Home / Java / Java Snippets / How to Read a File as Byte Array?

How to Read a File as Byte Array?

Read file as byte array in JavaIn order to get the contents of the file as array of bytes we use BufferedInputStream on top of FileInputStream. Once the file handle is open, we read the bytes from the file in chunks and copy the byte chunks in the buffer using the System.arraycopy.<!>

The Snippet

The Ouput

Example, read the content of an image file, and display the size.

java C:imagesIMG_0002.jpg

DEBUG>[FileToArray]-About to Read file : C:imagesIMG_0002.jpg
INFO>[FileToArray]-File Size : 1242702


Check Also

How to progamatically get the Java version?

Snippet The key here is to use the system property java.version and get minor and …

Advertisment ad adsense adlogger