Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*** In Java *** I need the rest of this assignment completed. I have it 90% done, I just cant for the life of me

*** In Java ***

I need the rest of this assignment completed. I have it 90% done, I just cant for the life of me figure out the rest.

What I need done:

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.

My code:

BinaryFileExplorer.java

import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.util.Scanner; import java.util.ListIterator;

/** * */ 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.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.print("Enter byte positon to navigate to: "); long loc = in.nextLong(); in.nextLine(); if (loc >= 0 && loc < fileList.size()) { ListIterator lit = fileList.listIterator((loc - 5 >= 0)?(int)(loc-5):0); int start = lit.nextIndex(); System.out.println("The surrounding 5 bytes (left and right) are: "); while (start <= loc+5 && start < fileList.size()){ if (start == loc){ System.out.print(" ( "); } byte b = lit.next(); System.out.print(((b >= 0 && b <= 15)?"0":" ") + Integer.toHexString((int)b & 0x00FF) + " "); if (start == loc) System.out.print(" ) "); System.out.print(" "); start++; } System.out.print("Would you like to edit the byte at this position?(y/n): "); String choice; choice = in.nextLine(); if (choice == "y"){ System.out.print("Enter the new hex value: "); String hv = in.nextLine(); int i = Integer.decode("0x" + hv); } } System.out.println(); System.out.println("Would you like to modify the byte value at this position (y/n): "); String choice = in.next(); System.out.println(); if("y".equals(choice)){ System.out.println("Enter a byte value as two HEX digits: "); Byte hex = in.nextByte(); 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("Cannot 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."); } } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 3 Lnai 6323

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

3642159389, 978-3642159381

Students also viewed these Databases questions