Reading Binary Data

InputStream is = new FileInputStream(fileName);
											int offset = 0;
											int bytesRead =
											is.read(bytes, offset, bytes.length-offset);

Using the read() method, we can read binary data from a file into an array of bytes. In this phrase, we read from the is input stream into the bytes byte array. In this phrase, the bytes array is assumed to have been previously initialized as a byte array, and the fileName variable is the name of a valid file. The offset variable points to a starting location in the bytes array to begin writing the data to. This is useful when you are in a loop reading data from a file and you don’t want to overwrite previous data stored in the byte array. For each iteration through the loop, you would update the offset ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.