CHAPTER 86 — Reading Binary Files

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to home page   go to next page        

created: 06/20/00; revised: 07/08/02

CHAPTER 86 — Reading Binary Files

Recall that binary files are those whose bytes might hold any possible pattern. Binary files store data that has not been translated into character form. Of course, a binary file can store character data mixed in with its other data types. A file that exclusively stores character data is called a text file.

The subclasses of InputStream are designed for byte-oriented input from any source. Many types of data sources of data can serve as an InputStream. However, in this chapter, the source will be a file on the hard disk.

Chapter Topics:

  • Reading binary files.
  • Advantages of binary files.
  • DataInputSteam
  • FileInputSteam
  • BufferedInputSteam
  • Reading and writing integers.
  • Reading and writing single bytes.

QUESTION 1:

You have found an old hard drive in a box of junk hidden behind a water cooler. On it, you find a mysterious file. It looks like it might be a file of 32-bit integers. Can you write a Java program to read it?