September 2019
Intermediate to advanced
816 pages
18h 47m
English
In the previous problem, Reading/writing text files efficiently, we talked about buffering streaming (for a clear picture, consider reading that problem before this one). Things work the same for binary files too, and so we can jump directly into some examples.
Let's consider the following binary file and its size in bytes:
Path binaryFile = Paths.get( "build/classes/modern/challenge/Main.class");int fileSize = (int) Files.readAttributes( binaryFile, BasicFileAttributes.class).size();
We can read the file's content in a byte[] via FileInputStream (this doesn't use buffering):
final byte[] buffer = new byte[fileSize];try (InputStream is = new FileInputStream(binaryFile.toString())) { int i; while ...