Question
***** IN JAVA ***** Write a program that prompts the user to enter the name of a binary file. If the file exists, store it
***** IN JAVA *****
Write a program that prompts the user to enter the name of a binary file. If the file exists, store it as an AList of Bytes. Prompt the user to enter a position in the file, and navigate to that position in the list. Use an iterator to print the 5 bytes before and the 5 bytes after. Ask the user if they want to change the value in that byte position. If yes, allow the user to type in a byte value AS A TWO DIGIT HEX Replace the value in the position with the new value. Repeat this until the user specifies a position of -1. Allow the user to save the binary file TO A NEW FILE LOCATION. Do NOT allow the user to save to a currently existing file. Use a hex editor to verify that the file was correctly written and the changes were made.
I have this to help:
import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.util.Scanner; /** * */ public class BinaryFileExplorer { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); System.out.println("Enter name of file: "); String filename = in.nextLine(); File file = new File(filename); if (file.exists()) { System.out.println(file.getAbsoluteFile()); RandomAccessFile raf = new RandomAccessFile(file, "r"); System.out.println("Size: " + file.length() + " bytes."); System.out.print("Enter byte positon to navigate to: "); long pos = in.nextLong(); in.nextLine(); if (pos >= 0 && pos < file.length()) { raf.seek(pos); System.out.println("The 5 bytes starting from that position are"); for (int i = 0; i < 5; i++) { byte b = raf.readByte(); System.out.print(((b >= 0 && b <= 15)?"0":"") + Integer.toHexString((int)b & 0x00FF) + " "); if (++pos == file.length()) break; } } System.out.println(); System.out.println("Attempting to record file to AList"); AList fileList = new AList<>(); raf.seek(0); for (int i = 0; i < raf.length(); i++) { fileList.add(raf.readByte()); } System.out.println("File saved in list"); for (byte b : fileList) { System.out.print(((b >= 0 && b <= 15)?"0":"") + Integer.toHexString((int)b & 0x00FF) + " "); } System.out.print("Enter name of save file: "); filename = in.nextLine(); File outputFile = new File(filename); if (outputFile.exists()) { System.out.println("Connot overwrite existing file"); System.exit(1); } DataOutputStream fout = new DataOutputStream(new FileOutputStream(outputFile)); for (byte b: fileList) { fout.write(b); } fout.close(); } else { System.out.println("File not found."); } } }
edit: I'm not entirely sure what information you need on top of this. Do you mind specifying?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started