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 mainString arpgs
JFileChooser fc new JFileChooser;
int returnVal fcshowOpenDialognull;
if returnVal JFileChooser.APPROVEOPTION
File file fcgetSelectedFile;
System.out.printlnFile contents: file.getAbsolutePath;
System.out.println;
showContentsfile;
private static void showContentsFile f
your code here to display file
This code snippet brings up a File Selection dialog box that starts in the USERHOME directory. On windows, this will be c:usersUSERdocuments The ifstatement 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 bytes at a time, and display the hexadecimal remember this number system from COP 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 bytes in hex first, then the ASCII charatter. For readability, put spacing between the hex and the ASCII. A example of printing the first lines of a file is shown below:
c c d a d c Dumbl
f c c edoreAlbu
c f sHogwarts
c e c e c 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 IntegertoHexString 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.formatx somebyte This will take a byte, and provide a digit hex representation of it The in the format specifier states that if the hex value is one digit, then left pad it with a So a byte value of will be F not F
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
