The program tests whether the file exists and exits if not. Otherwise, it creates a new file with a name that results from appending `.out` to the end of the file name the user supplied on the command line. E.g. if the user provides a name `test.txt`, the

computer science

Description

CSCI 1302 Homework #3

Due 11:30pm, Mar 7, Saturday, 2020

 

(98pts) Write a Java program that takes one command line argument that is supposed to be a file name entered by the user and do the following:

(1)   The program tests whether the file exists and exits if not. Otherwise, it creates a new file with a name that results from appending `.out` to the end of the file name the user supplied on the command line. E.g. if the user provides a name `test.txt`, then the new file name should be `test.txt.out`.

(2)   The program reads the bytes of the user supplied file one by one and displays them to stdout side by side with the new bytes to be written into the new file in hexadecimal format. For each byte in the user supplied file b7b6b5b4b3b2b1b0, the new byte should be x7b6x5b4b3x2b1x0, where x7, x5, x2 and x0 are the negations of b7, b5, b2 and b0 respectively (i.e. 0 should be negated to 1 and 1 should be negated to 0). For nice display, the program can display eight bytes of the original file on each line and separate them appropriately. Each displayed byte of the new file can follow the corresponding the byte of the original file and be inside a pair of parentheses. The program should write the new bytes into the new file while reading the bytes of the original file and displaying them to stdout. Once all contents of the original file have been read and displayed, report to stdout the number of bytes read from the original file.

(3)   Now that the program has finished reading the original file and writing the new file, it then reads the second half of the newly created file byte by byte and displays them to stdout in hexadecimal format. The program can also display eight bytes on each line for nice display. Once the second half of the new file have been all read and displayed, report to stdout the number of bytes read from the new file. E.g. if the original file contains 100 bytes, then the reported number of bytes read from the new file should be 50.

 

Sample run of the program can look like the following:

C:\>java TestHexXorFile testFile.txt

Original file and (new file contents) in hexadecimal format:

61 (C4) 62 (C7) 6F (CA) 75 (D0) 74 (D1) 20 (85) 31 (94) 37 (92)

5F (FA) 42 (E7) 69 (CC) 6E (CB) 61 (C4) 72 (D7) 79 (DC) 5F (FA)

49 (EC) 4F (EA)

Original file testFile.txt has size: 18 bytes.

 

Now read second half of the new file testFile.txt.out back in hexadecimal format:

(E7) (CC) (CB) (C4) (D7) (DC) (FA) (EC)

(EA)

read from new file testFile.txt.out 9 bytes.

 

Program Implementation Requirements & Hints:

Ø  You are recommended to use the File, FileInputStream, FileOutputStream and RandomAccessFile classes to achieve the goals. Your own class can have a name of your choice.

Ø  You may consider using other I/O classes mentioned in module 17_Binary_IO. Try not to use any I/O classes not mentioned in that module.

Ø  You may check out the static format method of the String class for displaying a byte in hexadecimal format. The formatting string used for the sample run above is `%02X`.

Ø  You may consider using the bitwise ^ operator to flip (i.e. negate) the requested bits in a byte. In Java a hexadecimal integer can be written beginning with `0x`. E.g. 0xe is the same as the decimal integer 14, which is also the same as the binary integer 1110.

 

Question (2pts): Based on the byte adaptation rule when creating the new file, is it possible to read the newly created file alone and recover the original file by another suitable adaption rule? You only need to answer YES or NO.

Submit your program (the .java source file(s)) to the dropbox a3 folder by deadline. Include as a comment at the top of your main source file your answer to the question. Do not send any screen shot. Do not send any .pdf file, or MS Word file, or any similar Word-Processing file, or .class file.


Related Questions in computer science category