Back to 111 Main Page

Mobius strip

Assignment 15

Task

Explore file I/O, exception handling, and command line arguments.

Steps

Write 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 Output

D:\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 Assignment15.java file to an email.

FAQs

Do you have any examples of this sort of thing?
Here is an example of command line arguments and writing to a file: FileAppender.java.
How do I read in from a file?
This code reads in each line of a file and prints it to the screen.
  try {
    Scanner filein = new Scanner(new File("input.txt"));
    while (filein.hasNextLine()) {
      System.out.println(filein.nextLine());
    }
  }catch (FileNotFoundException fnfe) {
    System.out.println("Could not open file: " + fnfe);
  }
How do I grab a random line of the file?
I recommend you just read the file line by line into an ArrayList. Then use Math.random() to grab a random line from the list.

Grading

Out of 10 points:

1 - Submission and Coding Standards
Follows required submission policies and coding standards.
2 - Compiles
2 - Command line arguments
Error/usage message on none or more than two; reads file if given one; reads file and appends to log file if given two;
2 - Displays random single line of given file
2 - Appends to log (if requested)
1 - Program does not crash due to IOExceptions


~ztomasze Index : TA Details: ICS111: Assignment 15
http://www2.hawaii.edu/~ztomasze
Last Edited: 22 Apr 2008
©2008 by Z. Tomaszewski.