JamesMadisonUniversity

Computer Science Department


CS 139 Lab:Experimenting with File I/O


Objectives:

To gain practice in working with file I/O in java.

Background:

Computing often involves the need to process input from a file and output data to a file. This lab will provide practice in reading from and writing to files.

New Terms:

EOF - End of File
java.io package - A Java package containing IO classes
File class - A class that contains the name of a file.
PrintStream class - A class that provides ways to produce output for a file.
CheckedException - A category of exceptions which must be handled in some way in the method in which it is called.

Materials

and getting

started:

  1. Set up a directory for this lab.
  2. Set up your programming environment.
  3. Download the following files:

    worksheet.txt
    Remember.java
    birthdays.txt
    assignments.txt

    to your working directory. (In most browsers, the easiest way to do this is by right-clicking on each of the links.)

    NOTE to Instructor: Update birthdays.txt and assignments.txt with current date entries.

Turning in
your work:

Answer as many of the following questions as you can during the lab period. You may work with others in the lab, or may seek help from the instructor or TA. 

Type your answers in the worksheet (see materials).  When finished, check your answers against the answer key.  If you are unable to complete the assignment during the lab period you must complete it on your own.   See your instructor for how to get credit for the lab.


Part 1 Getting Ready:

Before going any further you should:

  1. Make a directory for this lab.
  2. Setup your development environment.
  3. Download the files found in the Materials section above to your working directory. (In most browsers, the easiest way to do this is by right-clicking on each of the links.)
  4. Review the code and also add any other inline comments you need to help you understand what the code does. DO NOT CHANGE ANYTHING ELSE OR COMPILE YET.

Part 2 File Input Basics:

This part of the lab will help you understand file input.  Use the Java API to find more information about the File, Scanner, and PrintStream classes.

  1. Open Remember in the editor (e.g. jGrasp).
  2. Compile Remember.
  3. Execute Remember from the terminal window using the command:
     java Remember < birthdays.txt
        
  4. What output was generated?

  5. The Remember program is set up to read from the keyboard (System.in).  But the < symbol overrides standard input letting us read in the values in birthdays.txt instead of having to type them all in. Notice in the program that there is a PrintStream object which is used for output.  It has been instantiated to use System.out or standard output (monitor output).  System.out is a PrintStream object so we are using the constructor that accepts a PrintStream object as a parameter.  We will see later how to create a PrintStream object to print to a file instead of to the monitor.

  6. Execute Remember from the terminal window using the command:
     java Remember < anniversarys.txt
        
  7. What occurred and why?
  8. Add the declaration of a File variable named dates to main().
  9. What code did you need to add?
  10. Instantiate the variable named dates passing in the first command-line argument.  If there are no command-line arguments, display an error message and terminate.  See the Java API for File.  
  11. What did you need to add?
  12. Modify main() so that dateScanner uses dates.  Scanner has a constructor that takes in a File object.
  13. Compile Remember.
  14. What error message did you get?
  15. This is an example of a checked exception.  Look at the Scanner class documentation in the Java APIs.  Find the constructor that accepts a File object as its parameter.

  16. Checked exceptions have to be handled at the point that they occur or may be passed on to the calling method.  To pass the Exception, you would put a throws clause in the method header.  To handle the exception, you would build a try/catch block.  In this case, the exception would be thrown if the file specified in the File object cannot be found.  
      
  17. Add a try/catch block to catch the exception.  In the catch block, print an error message which tells the name of the missing file and then exit the program.
  18. What changes did you need to make?
  19. Execute Remember from the terminal window using the command:
     java Remember birthdays.txt
        
  20. What output was generated?

Part 3 More on File Input:

This part of the lab will give you more experience working with file input.

  1. Modify main() so that it will process multiple files when their names are passed-in as command-line arguments.
  2. What changes did you need to make?
  3. Execute Remember from the terminal window using the command:
     java Remember birthdays.txt assignments.txt
        
  4. What output was generated?
  5. Check your work against the Answer key and sample program - Remember.java.

    Optional: Not Required - Exercises to stretch your skills. Programming Practice After the Lab (Do not turn this part in)


    Here are some small assignments that you can complete (on your own time) to get more practice.

    1. Modify Remember so that it checks the dates when it reads them and writes invalid dates to a file with the same name as the input file but with a file type of ".err" (on Windows type is the same as extension). This is your error log.
    2. Further modify Remember so that it checks for the existence of the error log and prompts the user before overwriting it.
    3. Write an application named Untab that is passed a file name as a command-line argument, renames that file so that it has an extension of ".tmp" reads the ".tmp" file, replaces all of the tab characters with four spaces, and writes the result to a file with the original name.
    4. Change the format string and Locale in the call to screen.printf() that prints the variable today in various ways. Try to achieve the same results using a Formatter object and the println() method. Now, try to achieve the same results using a DateFormat object and the println() method.