Starting from:

$29

Assignment 4 Java File class

Assignment 4 
Objective
This assignment should give you practice with accessing the file 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 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: • “-i” (for (i)nformation) with an optional file or 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 “-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 |]|−v |−c ] Since that text has to be small to fit 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 [|]|-v |-c ] 2. Information: When the user requests information using the “-i” option, they may pass a 2nd parameter indicating the file or directory they wish information on. This is handled in different ways, depending on whether it is a file or directory. If it is a file: • Print the absolute path to the file • Indicate whether the file can be executed • Print out the file size in bytes • Print out the last modified date of the file • 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 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 by filesize (lowest to highest) • See example output for details If the 2nd parameter is not a regular file or directory, print the error message: ”Error: Invalid File” 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. 2 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 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 [|]| -v |-c |-d ] 7. Hints: • Remember that file 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 modified date using the Java Date class along with a SimpleDateFormat class (this is a good place to start) • You could also print the last modified date using printf. Look at the Formatter class for a detailed listing of the date/time flags. Remember to use the ”<” flag as well to keep using the same parameter (so you don’t have to pass the date in multiple times). As an example: System.out.printf("%b %|]|-v |-c |-d ] Submitting Pack all of your files (class files and source code) into a fully runnable JAR file called hw4.jar. The main program that the jar file should execute should be in class FileViewer. I should be able to run the main() method from your file with the command: java -jar hw4.jar Submit your jar file via the Blackboard submission link for assignment 4. 6

More products