Binary file I/O Lab
Objective: Working with binary files
Write a Hex File Viewer that can
display the contents of a file in hexadecimal notation. Start with this code. This example illustrates binary file i/o in Java. Your program will have
the following features:
- The File-Open option on the menu launches a JFileChooser (a file browser)
that allows the user to select a file to read. Click here
for a tutorial on using file choosers.
- Your program
- creates a File object infile attached
to this file
- creates a FileInputStream fis attached
to infile. You can use the fis object to determine how
many bytes are in the file input.dat. Look at the API to see
how.
- creates a byte array (byte [] b) of the
appropriate size.
- Uses the fis object to read in all the
bytes in the file into the byte array. How? See the
FileInputStream API.
- it runs through the array and displays the hexadecimal representation of
each byte read (how do I do this? See the API, for instance Integer.toHexString), in a JTextArea.
In your viewer, separate adjacent bytes
with a blank space.
To test your program: Use Notepad to save the characters ABC into a
file. Use your binary file viewer to read the file. As dicussed in class, if your program is working correctly, it should display 41 42 43.
Use your program to read and display the contents of this file. Can you determine what was written to the file
?