T-Th 9:05 |
CS 1110: Introduction to Computing Using Java Spring 2012 |
Main
About: Announcements Staff Consultants Times & Places Calendar Materials: Texts DrJava VideoNote Terminology Handouts: Lectures Assignments Labs Assessment: Grading Exams Quizzes Resources: CMS Piazza (link) Piazza (about) AEWs FAQ Java API Style Guide Academic Integrity |
Reading Files
Because of a last minute change in this lab, there is no handout for this lab. Sorry about
that. All of today's lab will consist of finishing the class This lab discusses input (e.g. reading a file). After the lab, study section 5.9 of the text. Better yet, listen to the lectures on lesson page 5-7 of the ProgramLive CD. The lectures are much clearer than the paper version. Requirements For This LabStart this lab by downloading file lab10.zip putting its contents in a new directory. It contains these files:
Once you have done that, open the As always, you should try to finish the lab during your section. However, if you do not finish during section, you have until the beginning of lab next week to finish it. You should always do your best to finish during lab hours; remember that labs are graded on effort, not correctness. Understanding Streams
A "stream" is a sequence of data values that is processed —
either read or written — from beginning to end. When the data is being
read, or input, the stream is called an "input stream"; when it is
being written, or output, the stream is called an "output stream".
Input/output of streams is done in Java using classes in package
The basic way to create an input stream for a file is by creating an instance
of class
And the standard way to read using
This is too low-level for us. We would like to be able to read not one character
at a time but one line at a time. For this, we use class
Instead of the above, use the following command:
The
reads the next line of the file and stores it in variable Using a JFileChooser Dialog Box
In order to read a file, you have to indicate which file should be read. The easiest way to do
this is to use a dialog window to navigate to the appropriate directory and select the file.
For this, we use an instance of class
A dialog box opens. Its title is "Choose input file". And it allows
you to navigate anywhere you want and then select a file. Do a bit of navigating
and select a file. Then take a look at function
The function for obtaining the next line from a
In the Interactions pane, you can continue to evaluate Processing the Lines of a File
Function Study this method. Any loop that you write that processes a file should be similar to this one —the "processing" of each line will change, but the basic structure of the loop that does the processing will not. Note that this method contains a while-loop. Here are important points:
The header of the method contains the following: Write Your Own MethodReading a File
Write the body of function
The specification of this method is including in
to see whether the Cornell netid djg17 appears in one of
the two files cms.txt and peop.txt .
This is the only part of the lab that is required. Please show your code to you instructor when you are done. Writing Files (Optional)
This section describes a task that you might have fun doing. It is something we had to do in
previous semesters when dealing with prelim conflicts. The task is to read in two files of netids,
say Please read on and learn about writing files, but you do not have to complete this task for this lab.
Writing files is not much different from reading them. Procedure Try this in the Interactions pane:
When the dialog window opens, navigate to a directory and select a file name or type in a completely different one; then you can select one and then change it slightly. Be careful. You will not want to overwrite an existing file, but this can easily happen. Now, type
The first statement writes a line to the file. You can write as many lines as you wish. The second statement should be used to close the file. After these statements are executed, look at the directory where the file was written, open the file, and look at it.
Now, write (and test) the body of procedure |