![]() |
Computer Science Department |
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. |
Materialsand gettingstarted: |
|
Turning in
|
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. |
Before going any further you should:
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.
Remember
in the editor (e.g. jGrasp).Remember
.Remember
from the terminal window using the command:
java Remember < birthdays.txt
Remember
from the terminal window using the command:
java Remember < anniversarys.txt
File
variable named dates
to main()
. 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.
main()
so that dateScanner
uses dates
.
Scanner
has a constructor that takes in a File
object.Remember
from the terminal window using the command:
java Remember birthdays.txt
This part of the lab will give you more experience working with file input.
main()
so that it will process multiple files when their names are passed-in
as command-line arguments.Remember
from the terminal window using the command:
java Remember birthdays.txt assignments.txt
Here are some small assignments that you can complete (on your own time) to get more practice.
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.Remember
so that it checks for the existence of the error log and prompts the
user before overwriting it.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.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.