M/F 2:30-3:20 |
CS 1130: Transition to OO Programming Spring 2016 |
Main
About: Overview Announcements Staff Consultants Calendar Materials: Texts DrJava Terminology Lectures: In-Class Web-Based VideoNote Assessment: Grading Assignments Labs Resources: CMS Piazza (link) Piazza (about) Java API Style Guide Academic Integrity |
Reading and Writing FilesThis 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. Start this lab by downloading file lab10.zip putting its contents in a new directory. It contains these files: Then open the .java file in DrJava and compile it. This is the file you will be working on for this lab. 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
Then, execution of
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 a new construct: 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
This section describes that 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 |