Assignment-4 Solution

$35.00 $29.00

Objective This assignment should give you practice with accessing the le system using the Java File class as well as the basics of using input/output streams. It will also give you more practice with command line arguments and some practice formatting dates. Task Write a le viewer that allows you to view le information, view…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Description

5/5 – (2 votes)

Objective

This assignment should give you practice with accessing the le system using the Java File class as well as the basics of using input/output streams. It will also give you more practice with command line arguments and some practice formatting dates.

Task

Write a le viewer that allows you to view le information, view directory struc-ture, read text les, and copy les. Your main program should be run from the le:

FileViewer.java

You should not need any other les or classes, but you may create them if you wish.

Each le should have a comment including your name at the top of the le. Each le should also have appropriate comments throughout.

Requirements

  1. Command Line Options: Your program should accept the following command line options:

\-i” (for (i)nformation) with an optional le or directory as a 2nd parameter. If no 2nd parameter is passed, default to the current directory \.”

\-v” (for (v)iew) with a le to view as the 2nd parameter

\-c” (for (c)opy) with a source le as the 2nd parameter and a desti-nation le as the 3rd parameter

If no parameters are passed, then the program should default to displaying information on the current directory (as if the user had passed \-i .”)

If the command is invalid usage (illegal options), print a usage message, as below, then exit the program:

Usage : j a v a j a r hw4 . j a r [ i [< f i l e >j<d i r e c t o r y >]j v <f i l e >j c <s o u r c e F i l e > <d e s t F i l e >]

Since that text has to be small to t on the line, I’ll break it up into multiple lines here (make sure you print it all on one line in your code):

1

Usage: java -jar hw4.jar

[-i [<file>|<directory>]|-v <file>|-c <sourceFile> <destFile>]

  1. Information: When the user requests information using the \-i” option, they may pass a 2nd parameter indicating the le or directory they wish information on. This is handled in di erent ways, depending on whether it is a le or directory. If it is a le:

Print the absolute path to the le

Indicate whether the le can be executed Print out the le size in bytes

Print out the last modi ed date of the le

Your output should match mine exactly (see the example output for the exact format)

However, if it is a directory:

Print a heading indicating that you are printing size followed by lename

Print each le/directory contained in the directory being viewed

For each le that you are printing, print the size, followed by the lename

For directories, print the size as *

Sort the les by lesize (lowest to highest) See example output for details

If the 2nd parameter is not a regular le or directory, print the error mes-sage:

“Error: Invalid File”

  1. View: If the user requests to view a le, the 2nd parameter should be a regular le. If the le is not found, print an appropriate error message. You should then print the contents of the le, as text, to the screen. Remember to handle all exceptions you encounter.

  1. Copy: If the user requests to copy a le, the 2nd parameter should be the name of a regular le (the source le) and the 3rd parameter should be the name of the le to copy to (the destination le). If the destination le already exists, do not make the copy and print an appropriate error message. If the source le is not valid, print an appropriate error message. Then you should copy the information from the source le to the destina-tion le, handling any exceptions encountered appropriately. After this, print a message to the user stating that the copy was successful. Please note that you should be able to copy binary les with this method, not just text les.

2

  1. Other Requirements:

Make sure you handle all exceptions that could be thrown when deal-ing with the le I/O

When printing error messages to the user, you do not have to match my format exactly (although it is preferred)

For all other output, match my format and text exactly

  1. Extra Credit: You may earn extra credit on this assignment by allowing

the user to specify an additional command-line option: “-d” for compare (d)i erences in les. This option should take 2 more parameters, both of which should be les. It should compare the two les and print a message indicating whether they are the same. As in the other options, handle exceptions. Please note that this changes the usage message. The new usage message is as follows (Please note this is on multiple lines, but your actual message should all print on the same line)

Usage: java -jar hw4.jar [-i [<file>|<directory>]|

-v <file>|-c <sourceFile> <destFile>|-d <file1> <file2>]

  1. Hints:

Remember that le sizes are returned via the length() function and are of type long (which can be converted to type Long using a class method of class Long – This can be very useful when attempting to sort)

You can print the last modi ed date using the Java Date class along with a SimpleDateFormat class (this is a good place to start)

You could also print the last modi ed date using printf. Look at the Formatter class for a detailed listing of the date/time ags. Remember to use the “<” ag as well to keep using the same param-eter (so you don’t have to pass the date in multiple times). As an example:

System.out.printf(“%b %<d %<Y”, date) would print out

Jan 31 2016

if the variable date was a long integer representing the date Jan 31, 2016.

The display option only has to handle text les, all other options should handle binary les as well (so use the appropriate streams).

You may use System.exit(0) to exit the program at any time

Make sure you handle all invalid commands (including too many command-line parameters)

3

Some Helpful Classes/Methods

String.equals(Object) String.charAt(int)

File class

Arrays.sort(T[], Comparator) Comparator Interface

Date(long)

DateFormat.format(Date) Long.valueOf(long)

Sample Output

(This example has the extra credit implemented)

(commands are underlined)

(these commands were all issued in order in the same directory)

java -jar hw4.jar

Size Filename

26 manifest.mf

  1. FileViewer$1.class

1018 aTextFile

1018 aCopyOfATextFile

*sol

6261 FileViewer.class

6683 hw4.jar

7140 FileViewer.java

java -jar hw4.jar -i ˜/cop3252/

Size Filename

  • another

  • classExamples

  • hw

  • exams

  • lec

  • cpdj

4

java -jar hw4.jar -i aTextFile

File Path: /home/grads/thrasher/cop3252/hw/hw4/aTextFile

Is executable? false

Size: 1018 bytes

Last Modified Date: Wed Nov 23 15:36:21

java -jar hw4.jar -v aTextFile

“Jabberwocky”

Twas brillig, and the slithy toves

Did gyre and gimble in the wabe;

All mimsy were the borogoves,

And the mome raths outgrabe.

“Beware the Jabberwock, my son!

The jaws that bite, the claws that catch!

Beware the Jubjub bird, and shun

The frumious Bandersnatch!”

He took his vorpal sword in hand:

Long time the manxome foe he sought

So rested he by the Tumtum tree,

And stood awhile in thought.

And as in uffish thought he stood,

The Jabberwock, with eyes of flame,

Came whiffling through the tulgey wood,

And burbled as it came!

One, two! One, two! and through and through The vorpal blade went snicker-snack! He left it dead, and with its head

He went galumphing back.

“And hast thou slain the Jabberwock?

Come to my arms, my beamish boy!

O frabjous day! Callooh! Callay!”

He chortled in his joy.

Twas brillig, and the slithy toves

Did gyre and gimble in the wabe;

All mimsy were the borogoves,

And the mome raths outgrabe.

5

— from Through the Looking-Glass, and What Alice Found There (1871)

java -jar hw4.jar -c aTextFile aCopyOfATextFile File aCopyOfATextFile already exists.

rm aCopyOfATextFile

java -jar hw4.jar -c aTextFile aCopyOfATextFile

File aTextFile was copied to file aCopyOfATextFile successfully.

java -jar hw4.jar -d aTextFile aCopyOfATextFile

The two files aTextFile and aCopyOfATextFile are the same.

java -jar hw4.jar -d aTextFile FileViewer.class

The two files aTextFile and FileViewer.class are not the same.

java -jar hw4.jar -q

Usage: java -jar hw4.jar [-i [<file>|<directory>]|-v <file>|-c <sourceFile> <des

Submitting

Pack all of your les (class les and source code) into a fully runnable JAR le called hw4.jar. The main program that the jar le should execute should be in class FileViewer. I should be able to run the main() method from your le with the command:

java -jar hw4.jar

Submit your jar le via the Blackboard submission link for assignment 4.

6

Assignment-4 Solution
$35.00 $29.00