CSCI 201 Lab 14 -- Input and Output Streams in Java

Introduction

So far this semester, we have learned quite a lot about computer programming and Java. We have learned basic Java program structure, primitive data types, expressions, objects, class libraries, control statements (conditional and iteration statements), instance variables, scope, parameters, returns, object references, interfaces, arrays, vectors, and inheritance. We have also learned how to create Java source code (*.java) files, which incorporate these concepts using a basic Integrated Development Environment (IDE) like JCreator LE .

This has been a lot of difficult material. You should be proud of yourself for making it this far. But, unfortunately, our ability to perform input and output (IO) in Java has been limited to input via the keyboard class and output via simple System.out.println() commands.

In this lab, we will take our knowledge regarding IO in Java to the next level. We will first learn how to write a program which obtains user input from the keyboard without using the keyboard class. This means we will learn how to manipulate Input Streams. Then we will learn how to write a program which sends program output somewhere else (in addition to the display screen), such as a file on a diskette or hard drive. This means we will learn how to manipulate Output Streams.

Part 1: Exceptions

The purpose of this part of the lab is to introduce you to writing programs in Java dealing with Exceptions. This includes Input and Output Streams. To start with, you will write a program that throws an Exception (Doesn't that sound fun!)

Getting Started

Now, let's discuss Exceptions a little bit. An Exception is an Object, therefore they can be created at any time. There are different types of exceptions each defined by a different class. You can design your own type of exception, and, as a matter of fact, we will be doing exactly that. After the code you have already typed, enter the following:


class GettingUpThereException extends Exception
{
	GettingUpThereException(String message)
	{
		super(message);
	}
}

What we have done is create a new type of exception that is derived from the parent (i.e., super) class Exception. The definition of the new derived class, GettingUpThereException, is placed within the Lab14 file. Now, we will complete the exceptional() method.

The final portion of the first part of this lab is to call the exceptional() method from main().


Lab Check-off 1

When you finish, show your program to your lab instructor.


Part 2: Input Streams

In the second part of this lab, you will learn how to read from the keyboard without the Keyboard class. Sounds fun, huh. Don't worry, it's not all that bad. We proceed by modifying Lab14.java as follows:


Lab Check-off 2

When you get your code running, show it to your lab instructor for credit.


Part 3: Output Streams

As our final modification to Lab14.java, we will write output to a file in addition to the monitor.

  • Run your program, and check the contents for the designated output file using Windows Explorer. The file will exist in the Lab14 directory but it will not appear in the JCreator FileView.

    Lab Check-off 3

    When you get your code to write to a file, show it to your lab instructor for credit.