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:
|
- Set up a directory for
this lab.
- Set up your
programming environment.
- 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:
- Make a directory for
this lab on your Novell (M) drive (and, if you wish, your flash drive)
- 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.)
- 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.
- Open Rooter.java
in the editor (e.g. jGrasp).
- Compile Rooter.
- What was the first
error generated by the compiler?
- Fix this error. What
line did you need to add?
- Compile Rooter.
- What was the first
error generated by the compiler this time?
- 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.
- 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.)
- Compile Rooter.
- Execute Rooter from the
terminal window using the command:
4. java Rooter
- Why was no output
generated?
- Fix this mistake. What
line did you add?
- What do you need to type
in the terminal window (as a response to the prompt) in order to make this
application terminate normally?
- 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.
- Open Remember in the editor (e.g. jGrasp).
- Add in the appropriate
header and other documentation, but do not change any code yet.
- Compile Remember.
- Execute Remember from the
terminal window using the command:
5. java Remember < birthdays.txt
- What output was
generated?
- Execute Remember from the
terminal window using the command:
8. java Remember < anniversarys.txt
- What happened and why?
- Add a declaration of a File variable
named dates
to main().
- What changes did you
need to make?
- Instantiate the variable
named dates
passing the first command-line argument.
- What did you need to
add?
- Modify main() so that dateScanner
uses dates
and the application will compile and execute properly.
- What changes did you
need to make?
- Execute Remember from the
terminal window using the command:
17. java Remember birthdays.txt
- What output was
generated?
Part 5 More on File Input:
This part of the lab will give you more experience
working with file input.
- Modify main() so that it will
process multiple files when their names are passed-in as command-line
arguments.
- What changes did you
need to make?
- Execute Remember from the
terminal window using the command:
4. java Remember birthdays.txt assignments.txt
- What output was
generated?
- 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.
- 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.
- Further modify Remember so that
it checks for the existence of the error log and prompts the user before
overwriting it.
- 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.
- 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.