Programming Assignment 1

File I/O, Console I/O, and Command-Line Arguments

Requirements

You will implement a Java console application that opens and reads the contents of text files specified as command-line arguments. For each file, the application writes to standard output the name of the file enclosed by square brackets. After writing the file name, the application writes the contents of the file with each line prefixed by its corresponding line number, a full colon, and a space.

For example, if a file with the name sample-input.txt contains these four lines:

This is the first line. This is the second line. This is the third line. This is the fourth line.

...then the application writes the following output if the user specifies sample-input.txt on the command line:

[sample-input.txt] 1: This is the first line. 2: This is the second line. 3: This is the third line. 4: This is the fourth line.

The application processes command line arguments in the order specified by the user. If the user enters no command line arguments, the application writes no output for the first part of this assignment. If the user specifies two or more files, the application writes a blank line between the output corresponding to each file.

The application then reads lines of standard input. For each line of input, if the user enters exit, the application terminates; otherwise, the application interprets the line as a path to a text file. The application creates or recreates this file and writes to it two lines of output, the name of the file and the current date and time. The application then closes the file, reopens it for reading, and writes its contents to standard output in the same manner as the files in the first part of this assignment.

The application writes the following prompt before attempting to read each line from standard input:

Enter a file name or type 'exit' to quit. -->

Hint: Be sure to flush standard output after writing the prompt.

If the application cannot read from standard input, it terminates without displaying an error. If any I/O errors occur while reading or writing files, the application writes a message to standard error and then continues to process user input.

Examples

$ java ChengJade1 sample-input.txt missing.txt [sample-input.txt] 1: This is the first line of the file. 2: This is the second line of the file. 3: This is the third line of the file. 4: This is the fourth line of the file. [missing.txt] missing.txt (The system cannot find the file specified) Enter a file name or type 'exit' to quit. --> output1.txt [output1.txt] 1: output1.txt 2: Wed Dec 28 10:13:00 HST 2011 Enter a file name or type 'exit' to quit. --> output2.txt [output2.txt] 1: output2.txt 2: Wed Dec 28 10:13:51 HST 2011 Enter a file name or type 'exit' to quit. --> exit

Resources