Programming Assignment 1

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

Example Implementation

/**
 * Implementation of Assignment 1: File I/O, Console I/O, and Command-Line
 * Arguments.
 *
 * @author     Cheng, Jade
 * @assignment CSCI 2912 Assignment 1
 * @date       Jan, 29 2012
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;
import java.util.Scanner;

/**
 * Implementation of Assignment 1: File I/O, Console I/O, and Command-Line
 * arguments.
 */
public final class ChengJade1 {

    /**
     * The main entry point of the program.
     * 
     * @param args The command-line arguments.
     */
    public static void main(final String[] args) {
        assert null != args;

        // Loop over every command-line argument. Each argument corresponds to
        // a file system path that is printed by this program.
        for (final String path : args)
            printFileContents(path);

        // Read standard input.
        final Scanner scanner = new Scanner(System.in);

        // Loop until the user types 'exit' or standard input is closed.
        while (true) {

            // Display the prompt.
            System.out.println("Enter a file name or type 'exit' to quit.");
            System.out.print("--> ");
            System.out.flush();

            // Check if standard input has been closed; if so, exit this loop.
            if (!scanner.hasNextLine())
                break;

            // Read the next line of input from the user.
            final String line = scanner.nextLine();

            // Check if the user has typed exit; if so, exit this loop.
            if ("exit".equalsIgnoreCase(line))
                break;

            // Check for errors when creating the file.
            try {
                // Create a file with the path specified by the user.
                final PrintStream out = new PrintStream(line);

                // Write two lines of text; the first line is the name of the
                // file, and the second line is the current date and time.
                out.println(line);
                out.println(new Date());

                // Close the file.
                out.close();

                // Print the contents of the file.
                System.out.println();
                printFileContents(line);

            } catch (final FileNotFoundException e) {
                // Display a message if there is an error creating the file.
                System.err.println(e.getMessage());
            }
        }
    }

    /**
     * Prints the contents of a specified file.
     * 
     * @param path The file system path to the file to print.
     */
    private static void printFileContents(final String path) {
        assert null != path;

        // Display a the path of the file before displaying its contents.
        final String title = '[' + path + ']';
        System.out.println(title);

        // Check for errors when reading the file.
        try {
            // Open the file for reading; use a scanner to read lines.
            final FileInputStream in = new FileInputStream(path);
            final Scanner scanner = new Scanner(in);

            // Loop over every line in the file.
            int i = 0;
            while (scanner.hasNextLine()) {
                // Display the line number and the contents of the line.
                final String line = scanner.nextLine();
                final String text = ++i + ": " + line;
                System.out.println(text);
            }

            // Close the file before returning.
            in.close();

        } catch (final IOException e) {
            // Display a message if there is an error reading the file.
            System.err.println(e.getMessage());
        }
        
        // Add a line of space between files that are printed.
        System.out.println();
    }
}