Question: Please help me with this: File Input Streams For this graded assignment, you will write a program that will use a FileInputStream to read bytes

Please help me with this:
File Input Streams
For this graded assignment, you will write a program that will use a FileInputStream to read bytes from a file, and display both a hex version of the byte, and it's corresponding ASCII code (if it is a printable character).
Details
This program should be able to read any file the user selects. To make this easy for the user to select a file, we're going to use the JFileChooser dialog. This is from the Swing framework (precursor to JavaFX). You can use the following code to bring up a dialog box that will allow you to navigate to a file on your computer:
public static void main(String arpgs[]){
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
System.out.println("File contents: "+ file.getAbsolutePath());
System.out.println();
showContents(file);
}
}
private static void showContents(File f){
// your code here to display file
}
This code snippet brings up a File Selection dialog box that starts in the USER-HOME directory. On windows, this will be c:\users\USER\documents. The if-statement checks to see if a file was selected, and if so, then calls a method named showContents (that you will need to write) and pass in the selected File object. You will need to write this method to show the contents of the file. This file can any type of file -- text or non text.
As mentioned earlier, you will use a FileInputStream to read bytes from the selected file. Your version of the showContents() method will read 10 bytes at a time, and display the hexadecimal (remember this number system from COP1000) representation of the byte, and the ASCII character, if it is a letter or digit. If it is not a letter or a digit, then just print a period ".". You should display the 10 bytes in hex first, then the ASCII charatter. For readability, put spacing between the hex and the ASCII. A example of printing the first 4 lines of a file is shown below:
7c 317c 0d 0a 44756d 626c .1...Dumbl
65646f 72657c 416c 6275edore.Albu
737c 486f 677761727473s.Hogwarts
7c 454e 477c 77616e 647c .ENG.wand.
There are many ways to get the hex representation of a number, and our text shows you one way with a wrapper class (Integer.toHexString). You can use this if you want, but there's a simpler way -- use the format specifier 'x'. For example, you can do something like this: String.format("%02x ", some_byte). This will take a byte, and provide a 2 digit hex representation of it. The '0' in the format specifier states that if the hex value is one digit, then left pad it with a '0'. So, a byte value of 15 will be 0F, not F.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!