CS 117: Files, mice, and keys
Winter 2005

In this lab, you will experiment with existing Java programs with the goal of learning how to use files, mouse clicks, and keystrokes in your programs.

Files

Suppose you have a file called words.txt that consists of one word per line, and you want to sort the words. An array or an ArrayList plus insertion sort can handle the computational job, but how do you get the words out of words.txt in the first place, and where should the results go when you're done? In this part of the lab, we're going to experiment with several approaches to this question.

Standard input and standard output

The Java object System.in is an object of type InputStream that is initialized for you whenever you run a Java program. Normally, System.in is set up to take input from the keyboard, but in most command-line systems, there's a way to cause System.in to take input from a file, instead. Similarly, System.out, an object of type PrintStream, is normally initialized to send output to the screen, but can be "redirected" to a file via the command line. Almost all programming languages provide you with a way to read from "standard input" and write to "standard output"; System.in and System.out are Java's way. In this section of the lab, you will run a sorting program in several ways to see how standard input and standard output work.

Redirecting standard input

Make yourself a working directory and copy all the files for today's lab to it. (The command "cp /Accounts/courses/cs117/ondich/fileslab/* ." should do the job.) We'll start work with WordSorter1.java.

Open WordSorter1.java and take a look at it. It uses an ArrayList to store the words, and insertion sort in the sort method. But take a look at the constructor, where we load the words from the file into the ArrayList. Note that we are using a Scanner based on System.in, which we normally use for the keyboard.

Now try the following.

The moral? You can "redirect" standard input to a file using the < character on the command line.

Not redirecting standard input

There's a handy technique that's a lot less common than redirecting standard input, if you want to test a program on a "file" that doesn't exist. Try the following.

Redirecting standard output

Now, let's send the output of WordSorter1.java to a file via redirection.

Using File objects for input and output

In many programs, it's not reasonable for all the input to come from a single file, nor for all the output to go to a single file. Think of a spell-checker, for example, which needs to take input from both a dictionary file and the file whose spelling you want checked. In such cases, you can't depend on command-line redirection.

Let's give it a try.

Mice

Mouse clicks are another kind of input used by programs. Java makes it quite easy to arrange for your programs to receive and respond to mouse clicks. In this section of the lab, we'll look at how this is done.

Take a look at ClickableWindow.java. There are several new ideas in this program:

That's a lot of unfamiliar stuff. But the heart of the matter is that the mouse-related methods in the ClickableWindow class will get called automatically when mouse events occur. To see it in action, try the following:

Keys

Keystrokes are handled in a way that's very similar to the handling of mouse events. Experiment with TypeableWindow.java to learn more about the KeyListener interface. Note that to get the program to do anything interesting, you have to type "r" or "g" or "b".


Written by Jeff Ondich.