![]() |
Assignment 15TaskExplore file I/O, exception handling, and command line arguments. StepsWrite a program that reads in a file and displays a random line of that file. This could be used for a number of uses--as a die roller, to display random quotes, etc. Here are a couple sample files for use as a die roller: d6.txt and d20.txt. Feel free to create your own input files too. Your program should take at least one filename as a command line argument. This is the file to open and read. If no command line arguments are given (or more than 2), give an error message and explain how to use the program properly. If a second command line argument is given, treat this as the name of a log file. Your program should append whatever line it prints out to this log file. Your program should catch all exceptions and explain the source of the error. Specifically, you might get a FileNotFoundException when trying to read from a file that doesn't exist or if you cannot open the second file to write to. If you catch an IOException, it will also catch all IOException subclasses, including FileNotFoundException. Normally, you need to translate what an exception means into something meaningful to the user. However, IOExceptions are pretty good, so you can simply include a print out of the exception object as explanation. To test your program, try opening a file that doesn't exist or writing to a file that's in use by a program that locks its files while editing them (such as MS Word). Sample OutputD:\TA\hw\A15>java Assignment15 No filename (or too many) specified on command line. Usage: java Assignment15 inputfile [outputfile] This program displays a random line from inputfile. If outputfile is also give, it also appends the random line to that file. D:\TA\hw\A15>java Assignment15 d6.txt 1 D:\TA\hw\A15>java Assignment15 d6.txt 5 D:\TA\hw\A15>java Assignment15 d6 Error -- Could not access file: java.io.FileNotFoundException: d6 (The system cannot find the file specified) D:\TA\hw\A15>java Assignment15 d6.txt log.txt 4 D:\TA\hw\A15>java Assignment15 d6.txt log.txt 3 D:\TA\hw\A15>more log.txt 4 3 D:\TA\hw\A15>java Assignment15 d6.txt log.txt Error -- Could not access file: java.io.FileNotFoundException: log.txt (The process cannot access the file because it is being used by another process) D:\TA\hw\A15> What to submit
Attach your FAQs
GradingOut of 10 points:
|
~ztomasze Index :
TA Details: ICS111:
Assignment 15 http://www2.hawaii.edu/~ztomasze |
Last Edited: 22 Apr 2008 ©2008 by Z. Tomaszewski. |