JamesMadisonUniversity

Computer Science Department


CS 239 -  Lab 5 :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:

flush() method (PrintWriter)
java.io package
File class
PrintWriter class

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
Rooter.java
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.)

 

Turning in
your work:

Answer as many of the following questions as you can during the lab period. You may ask each other questions during lab, or may seek help from the instructor or TA. Each student must turn in his/her own copy of the lab answers.

You must type your answers in the worksheet (see materials) and submit your answers using Blackboard . Also attach your final copies of the two programs, Rooter and Remember. (Make sure you attach the .java files.)

If you are unable to complete the assignment during the lab period you must complete it on your own and submit it by midnight tonight. .


Part 1 Getting Ready:

Before going any further you should:

  1. Make a directory for this lab on your Novell (M) drive (and, if you wish, your flash drive)
  2. 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.)
  3. Into each .java file, put the standard class header as specified by your instructor. Review the code and also add any other inline comments needed to explain what is happening. DO NOT CHANGE ANYTHING ELSE OR COMPILE YET.

Part 2 Using the Java APIs:

This part of the lab will help you remember how to use the Java APIs.

  1. Open Rooter.java in the editor (e.g. jGrasp).
  2. Compile Rooter.
  3. What was the first error generated by the compiler?
  4. Fix this error. What line did you need to add?
  5. Compile Rooter.
  6. What was the first error generated by the compiler this time?
  7. Fix this error. What line did you need to add?

Part 3 Keyboard Input and Screen Output:

This part of the lab will help you understand the basics of keyboard input and screen output.

  1. Open a terminal window (Linux) or the DOS command window (Windows) and make your working directory the directory you created for this lab. (Remember the cd command -- works in both command windows.)
  2. Compile Rooter.
  3. Execute Rooter from the terminal window using the command:
4.               java Rooter
    
  1. Why was no output generated?
  2. Fix this mistake. What line did you add?
  3. What do you need to type in the terminal window (as a response to the prompt) in order to make this application terminate normally?
  4. Make sure that your Rooter application is appropriately documented for this class. You will upload Rooter.java to the Blackboard assignment.

Part 4 File Input Basics:

This part of the lab will help you understand file input.

  1. Open Remember in the editor (e.g. jGrasp).
  2. Add in the appropriate header and other documentation, but do not change any code yet.
  3. Compile Remember.
  4. Execute Remember from the terminal window using the command:
5.               java Remember < birthdays.txt
    
  1. What output was generated?
  2. Execute Remember from the terminal window using the command:
8.               java Remember < anniversarys.txt
    
  1. What happened and why?
  2. Add a declaration of a File variable named dates to main().
  3. What changes did you need to make?
  4. Instantiate the variable named dates passing the first command-line argument.
  5. What did you need to add?
  6. Modify main() so that dateScanner uses dates and the application will compile and execute properly.
  7. What changes did you need to make?
  8. Execute Remember from the terminal window using the command:
17.           java Remember birthdays.txt
    
  1. What output was generated?

Part 5 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:
4.               java Remember birthdays.txt assignments.txt
    
  1. What output was generated?
  2. You should upload your completed Remember.java file along with your worksheet to the Blackboard assignment.

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.