Espresso: A Concentrated Introduction to Java

Laboratory: Simple Input and Output

Summary: In this laboratory, you will explore the basics of input and output in Java.

Classes Used:



Exercise 0: Preparation

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

d. Start Eclipse and tell it to load the project from the IO directory.

Exercise 1: Basic Interaction

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

To type a response, you may need to click in the console area of the Eclipse window.

Exercise 2: Creating a File

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 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 to write to a file in a classmate's directory.

f. Verify your prediction experimentally.

Exercise 3: Reading a File

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

the output on the screen should be

Name: J. Doe
Fave Color: plaid

b. Modify info using gedit and verify that ShowInfo continues to work correctly.

Exercise 4: Insufficient Input

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.

Exercise 5: Copying Parts of a File

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.

Exercise 6: Copying Parts of a File, Revisited

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.

Exercise 7: Rewinding a 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.

Extra Work

If you finish the lab early, you may want to try some or all of the following.

Extra 1: Marks and Keyboard Input

Determine whether you can use mark and reset for input taken from the keyboard in addition to input taken from a file.

Extra 2: Multiple Marks

Determine whether you can place more than one mark in a file.

Extra 3: Reading Ahead

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]

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

You may wish to validate this page's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky