Aqua Phoenix
     >>  Lectures >>  Java 6  
 

Navigator
   
 
       
   

6.4 Binary Files: Reading and Writing

Binary files are not very different from text files. Instead of interpreting the contents as readable characters, each byte is interpreted as a numerical value (0 - 255). The methodology of file streams applies to binary files as much as it does to text files.

However, there is no such encapsulation object as a BufferedReader or a BufferedWriter, because there is no concept of a line in a binary file. To this extent, binary files are easier to work with. In general, binary file streams work with arrays of bytes. There are no general encapsulations for this data type.

6.4.1 FileInputStream

A stream for reading data from a binary file is created using the class FileInputStream:

try {
  FileInputStream in = new FileInputStream(new File("binaryFile"));
} catch (Exception e) {
  e.printStackTrace();
}
To read data from this file, we will need to read it in chunks. While it is possible to read all of a file's data at once, it is generally not recommended. This method works well for small files, but as soon as file sizes range in the mega bytes, reading all contents into a byte array does require a lot of memory. Finally, if a file is in the giga byte range, it becomes infeasible to hold that data in memory.

byte[] data = new byte[1024];
int readBytes;
try {
  FileInputStream in = new FileInputStream(new File("binaryFile"));
  while ((readBytes = in.read(data)) != -1) {
    System.out.println("read " + readBytes + " bytes, and placed them into temp array named data");
  }
  in.close();
} catch (Exception e) {
  e.printStackTrace();
}
Sample output:

...
read 1024 bytes, and placed them into temp array named data
read 1024 bytes, and placed them into temp array named data
read 1024 bytes, and placed them into temp array named data
read 1024 bytes, and placed them into temp array named data
read 1024 bytes, and placed them into temp array named data
read 252 bytes, and placed them into temp array named data
Within the loop, as many as 1024 bytes are read from the file stream, and placed in byte array data. 1024 is an arbitrary number defined in the length of byte array data. However, not every binary file's size is a multiple of 1024, and thus the last chunk read is most often less than 1024. When reading data from a file into an array, method read() returns the actual number (int) of bytes read from the stream. Using this integer (named readBytes above), we know how many byte in array data are actually valid.

In each iteration of the loop, the contents of array data is updated and made available for further processing.

6.4.2 FileOutputStream

The counterpart to FileInputStream for reading is the FileOutputStream for writing binary data:

try {
  FileOutputStream out = new FileOutputStream(new File("binOutFile"));
} catch (Exception e) {
  e.printStackTrace();
}
To write data to this stream, it is possible to write individual bytes or arrays of bytes. In the following example, we generate 100,000 random individual bytes, and write them to the file:

try {
  FileOutputStream out = new FileOutputStream(new File("binOutFile"));
  for (int i = 0; i < 100000; i++) {
    out.write((byte)((Math.random() * 255) - 128));
  }
  out.close();
} catch (Exception e) {
  e.printStackTrace();
}
Instead of writing individual bytes, we can also write byte arrays:

byte[] b = new byte[1000];
try {
  FileOutputStream out = new FileOutputStream(new File("binOutFile"));
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < b.length; j++) {
      b[j] = (byte)((Math.random() * 255) - 128);
    }
    out.write(b, 0, b.length);
  }
  out.close();
} catch (Exception e) {
  e.printStackTrace();
}

6.4.3 Notes

  • Similar to the case of text files, it is equally important to close streams for binary files. If an output stream is not closed, the last buffer of data will not be written.
  • In the above examples, random data was used for file writing. Usually, the data will be provided by some other source, e.g. an Image, or the output of encrypting some text, etc.