Espresso: A Concentrated Introduction to Java
Summary: In this laboratory, you will explore the basics of input and output in Java.
Classes Used:
java.io.BufferedReader
java.io.FileReader
java.io.File
java.io.InputStreamReader
java.io.PrintWriter
Contents
a. Review Simple Input and Output.
b. Create a new directory for this lab, called /home/username/CSC152/IO
.
c. Within that directory, create the directories for the package username.io
.
d. Start Eclipse and tell it to load the project from the IO directory.
Write and test a main class called Greet
that prompts the
user for a name, reads a name from the keyboard, and prints a greeting
to the screen. Your output should resemble the following:
What is your name? Sam Have a nice day Sam!
To create a new class, select New and then Class from the
File menu. Make sure to indicate that the class belongs in
package username.io
.
To type a response, you may need to click in the console area of the Eclipse window.
a. Write and test a main class called DumpInfo
that
prints the following three lines to the screen.
b. Update DumpInfo
so that it instead creates a file
and dumps the information to that file.
c. Predict what will happen when you have DumpInfo.java
write to a file that already exists. (For example, if you run it a
second time.)
d. Verify your prediction experimentally.
e. Predict what will happen when you modify DumpInfo.java
to write to a file in a classmate's directory.
f. Verify your prediction experimentally.
a. Write and test a main class called ShowInfo
that reads a file
of three lines named info
and prints each line preceeded
by a category prompt (Name, Email, Color). For example, if the file contains
J. Doe doej@gmail.com plaid
the output on the screen should be
Name: J. Doe Email: doej@gmail.com Fave Color: plaid
b. Modify info
using gedit and verify that
ShowInfo
continues to work correctly.
a. What do you expect to happen when you run ShowInfo
if info
contains only two lines (e.g., doesn't contain a color)?
b. Verify your answer experimentally.
c. What do you expect to happen when you run ShowInfo
if info
does not exist?
d. Verify your answer experimentally.
e. What do you expect to happen when you run ShowInfo
using a file in a classmate's directory?
f. Verify your answer experimentally.
Write and test a main class, CopyTwoLines
, that copies
the first two lines from a file named source
and places
them in a file named destination
.
Write a test a main class, NewCopier
, that prompts the
user for the name of two files and copies the first two lines from the
first file to the second file.
Write a short main class, ReadTwice
, that prompts
the user for a file, builds a BufferedReader
for the
file, sets a mark, reads the first line from the file, resets the
BufferedReader
, and reads again.
If you finish the lab early, you may want to try some or all of the following.
Determine whether you can use mark
and reset
for input taken from the keyboard in addition to input taken from a
file.
Determine whether you can place more than one mark in a file.
Determine what happens if you read more than readAheadLimit
characters between calls to mark
and reset
. Does
the size of readAheadLimit
make a difference (e.g., do you
get the same behavior for small and large readAheadLimit
if
you read only two characters beyond that limit)?
Monday, 31 January 2005 [Samuel A. Rebelsky]
Sunday, 4 September 2005 [Samuel A. Rebelsky]
when things go wrongquestions.
Tuesday, 31 January 2006 [Samuel A. Rebelsky]
This page was generated by
Siteweaver on Thu Mar 30 15:24:38 2006.
The source to the page was last modified on Tue Feb 7 08:43:54 2006.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Espresso/Labs/io.html
.
You may wish to
validate this page's HTML
;
;
Check with Bobby