I am trying to print the hexadecimal and then the ASC11, but it is not displaying correct. Can you please review my code nad provided feedback. Current format : File contents: C:\Users\shari\OneDrive\Documents\testfile.dat Hexadecimal:0000000001 ASC11:2 ASC11:3 ASC11:4 ASC11:5 ASC11:6 ASC11:7 ASC11:8 ASC11:9 ASC11:10 Requirment dispaly example : 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 31 7c 0d 0a 44 75 6d 62 6c .1...Dumbl 65 64 6f 72 65 7c 41 6c 62 75 edore.Albu 73 7c 48 6f 67 77 61 72 74 73 s.Hogwarts 7c 45 4e 47 7c 77 61 6e 64 7c .ENG.wand. 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 package cop2251.fall23.week3.sullivan; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.swing.JFileChooser; import java.io.*; //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). public class Files{ 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); } }//Input File is read in binary and output files print in Hex and ASC11 private static void showContents(File file){ try ( //Create an input file stream using try/catch block. This will automate the closing of the file. //FileInputStream Reads files FileInputStream incomingFile = new FileInputStream (file); ) {//create a variable to read the file int valueH; // values from the file to Hex int valueA;//values to hold ASC11 while ((valueH = incomingFile.read()) != -1) //read the file as long as it is not the end of the file. {//Print Hex // /You should display the 10 bytes in hex first, then the ASCII charatter. System.out.println(String.format("Hexadecimal:" + "%10s", Integer.toHexString(valueH)).replace(' ', '0')); //Print Hex to ASC11 while ((valueA = incomingFile.read()) != -1) //read the file as long as it is not the end of the file. {//Print Hex System.out.println(String.format("ASC11:" + Integer.toString(valueA))); } //Print ASC11 // System.out.println((char)value); } //temp varaible outside prinnt stagtement > covert to bi > use print statement > outside print statement Convert tp ASC11> Print statement check to //Try/Catch Block closes the file. } catch (IOException e) { e.getMessage(); } } }
File contents: C:\Users\shari\OneDrive\Documents\testfile.dat
Hexadecimal:0000000001
ASC11:2
ASC11:3
ASC11:4
ASC11:5
ASC11:6
ASC11:7
ASC11:8
ASC11:9
ASC11:10
Requirment dispaly example :
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 31 7c 0d 0a 44 75 6d 62 6c .1...Dumbl
65 64 6f 72 65 7c 41 6c 62 75 edore.Albu
73 7c 48 6f 67 77 61 72 74 73 s.Hogwarts
7c 45 4e 47 7c 77 61 6e 64 7c .ENG.wand.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images
I am getting an error at my while statement. It says Type mismatch and wants me to change the vaule type to bollean vs int. But if I do that the greater than function does't work. Do you see any errors in my code ?
public class Files{
public static void main(String arpgs[]) {
JFileChooser fc = new JFileChooser();
intreturnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
System.out.println("File contents: " + file.getAbsolutePath());
System.out.println();
showContents(file);
}
}//Input File is read in binary and output files print in Hex and ASC11
private static void showContents(File file){
//Create an input file stream using try/catch block. This will automate the closing of the file.
//FileInputStream Reads files
try ( FileInputStream incomingFile = new FileInputStream (file))
{//create a variable to read the file )
intvalue =0;
intcount = 0;
while (value= incomingFile.read())!= -1){
//Print as Hex and ASC11
System.out.printf("%02X ", value); //
System.out.print((char) (value >= 32 && value <= 126 ? value : '.'));
count++;
if(count % 10 ==0)
{
System.out.println();
}
}
// Print ASCII character if printable, else '.'
//Print Hex and ASC11 using printf %02X = HEX with leading zeros
// increase the count
//Try/Catch Block closes the file.
} catch (IOException e) {
//print error message
e.getMessage();
}
}
}