Question: Write a file viewer that allows you to view file information, view directory structure, read text files, and copy files. Your main program should be
Write a file viewer that allows you to view file information, view directory structure, read text files, and copy files. Your main program should be run from the file: FileViewer.java You should not need any other files or classes, but you may create them if you wish. Each file should have a comment including your name at the top of the file. Each file should also have appropriate comments throughout. Requirements 1. Command Line Options: Your program should accept the following command line options: -l (for (l)isting) with an optional directory as a 2nd parameter. If no 2nd parameter is passed, default to the current directory . -v (for (v)iew) with a file to view as the 2nd parameter -c (for (c)opy) with a sourcefile as the 2nd parameter and a destination file 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 -l .) 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 [? l [< d i r e c t o r y >]|?v |?c ] Since that text has to be small to fit on the line, Ill break it up into multiple lines here (make sure you print it all on one line in your code): Usage: java -jar hw4.jar [-l []|-v |-c ] 1 2. Information: When the user requests information using the -l option, they may pass a 2nd parameter indicating the directory they wish a listing for. Then: Print a heading indicating that you are printing size followed by filename Print each file/directory contained in the directory being viewed For each file that you are printing, print the size, followed by the filename For directories, print the size as * Sort the files/directories by filesize (lowest to highest) See example output for details If the 2nd parameter is not a valid directory, print the error message: Error: Invalid Directory 3. View: If the user requests to view a file, the 2nd parameter should be a regular file. If the file is not found, print an appropriate error message. You should then print the contents of the file, as text, to the screen. Remember to handle all exceptions you encounter. 4. Copy: If the user requests to copy a file, the 2nd parameter should be the name of a regular file (the source file) and the 3rd parameter should be the name of the file to copy to (the destination file). If the destination file already exists, do not make the copy and print an appropriate error message. If the source file is not valid, print an appropriate error message. Then you should copy the information from the source file to the destination file, 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 files with this method, not just text files. 5. Other Requirements: Make sure you handle all exceptions that could be thrown when dealing with the file 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 6. 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)ifferences in files. This option should take 2 more parameters, both of which should be files. It should compare the two files and print a message indicating whether they are the same. As in the other options, handle 2 exceptions and print error messages for invalid arguments/files. 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 [-l []| -v |-c |-d ]
Sample Output
java -jar hw4.jar Size Filename 23 manifest.mf 767 FileViewer$1.class 1018 aTextFile * sol * testing 5834 FileViewer.class 6293 hw4.jar 6448 FileViewer.java java -jar hw4.jar -l /cop3252/ Size Filename * another * examples * classExamples * hw * testing * exams * lec * cpdj java -jar hw4.jar -l aTextFile Error: Invalid Directory 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!" 4 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. -- from Through the Looking-Glass, and What Alice Found There (1871) java -jar hw4.jar -c aTextFile aCopyOfATextFile File aTextFile was copied to file aCopyOfATextFile successfully. java -jar hw4.jar -c aTextFile aCopyOfATextFile File aCopyOfATextFile already exists. 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 -c abcd copied Invalid filename, please try again java -jar hw4.jar -q Usage: java -jar hw4.jar [-l []|-v |-c |-d ] java -jar hw4.jar 5 Size Filename 23 manifest.mf 767 FileViewer$1.class 1018 aTextFile 1018 aCopyOfATextFile * sol * testing 5834 FileViewer.class 6294 hw4.jar 6448 FileViewer.java
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
