Espresso: A Concentrated Introduction to Java
Summary: We introduce (or re-introduce) the basics of input and output in Java, including input from the keyboard and files, and output to the screen and to files.
Prerequisites: Basics of Java.
In the early days of computing, programs were run only in batch mode. That is, you would start the program, wait for it to compute results (or crash), and then read the results (or error log). Initially, programmers coded any values the program would use directly into the program. Soon thereafter, however, they realized that it was far more general to place the input to a program (and the output from a program) in separate files.
With the advent of interactive computing systems, it became even more important for programs to be able to read input from a variety of sources and to send output to a variety of targets. In early interactive systems, most input was based on text that came from the keyboard and that went to a monitor or printer that showed only alphanumeric characters. Hence, the focus of early input and output commands was textual input and output.
Event with the creation of modern window-based systems, in which input can come from mice, touchscreens, and a variety of other devices and output can be graphical and auditory as well as textual, it is still useful to be able to read and write text, if only to save comments to a log and to gather data from a file.
In many of the exercises in Espresso, we will focus on text-based input and output. It is therefore important to learn textual input and output. In this reading (and the corresponding lab), we will explore the basics of textual input and output.
You should already know most of what you need to know about simple output
in Java. The primary kind of object for output (at least the most basic
output) is a
java.io.PrintWriter. You can create PrintWriters
that print to the screen and PrintWriters that print to a file.
To create a PrintWriter for the sceen, use something the following.
java.io.PrintWriter pen; pen = new java.io.PrintWriter(System.out, true);
Two shorthands can simplify this code. First, if you import
java.io.PrintWriter, you need only provide the basic class name. Second, you can put assignment on the same line as declaration. The result is therefore
PrintWriter pen = new PrintWriter(System.out, true);
Preparing a PrintWriter for a file is nearly as simple. First, you must create an object in class
java.io.File. Second, you build a new
PrintWriter with that file as a parameters. In verbose form,
java.io.File outfile; java.io.PrintWriter pen; outfile = new java.io.File("name-of-file"); pen = new java.io.PrintWriter(outfile);
Custom suggests that you
close a File when you're done with it.
One benefit of creating the same kind of object for either kind of output is that it's very easy to update your program to write to a file instead of to the screen (or vice versa).
PrintWriters provide a few basic methods:
print(what-to-print): Queue for printing the given value to the appropriate destination. Does not print a carriage return, so subsequent output will appear on the same line.
println(what-to-print): Print the given value to the appropriate destination followed by a carriage return. Normally
println(): Print a carriage return. Normally
flush(): Finishes printing anything queued by
Many programmers find the delayed printing of
wait until there's enough
to write the default behavior and provided
for those who want different behavior.
Most simple input in Java is done using objects from class
java.io.BufferedReader. As you might expect,
you can create buffered readers that read from the keyboard and
buffered readers that read from files. However, before creating
you must create an intermediate object, either an
java.io.InputStreamReader or a
For example, to create an object to read from the keyboard, you would use code like the following
java.io.BufferedReader eyes; java.io.InputStreamReader istream; istream = new java.io.InputStreamReader(System.in); eyes = new java.io.BufferedReader(istream);
Similarly, to create an object to read from a file, you would use code like the following
java.io.File infile; java.io.FileReader istream; infile = new java.io.File(name-of-file); istream = new java.io.FileReader(infile); eyes = new BufferedReader(istream);
Note that the names you choose for your objects need not match the ones that I've chosen.
BufferedReaders provide one basic method:
This method reads the next line of input and returns it as a string.
The line read does not linclude the carriage return or line feed that
typically ends lines. You typically assign the line read to a variable,
// Declarations ... String name; ... // Commands ... name = eyes.readLine();
You may, of course, also use the line read as you may use any string, such as to print it out directly.
When you're done with input, you should close it, just as you close output.
One nice part about
BufferedReader objects is that they
permit you to
back up in the input file. As you will note from
the documentation, you can use
mark to indicate a place
to which you will rewind and
reset to rewind to that place.
mark takes one parameter,
a number that indicates the maximum number of characters that will be read
before the next call to
reset. For example, here is some
silly code that reads two lines from a file and then prepares to read the
first of the two lines again.
eyes.mark(1024); pen.println(eyes.readLine()); pen.println(eyes.readLine()); eyes.reset();
Sunday, 30 January 2005 [Samuel A. Rebelsky]
Monday, 31 January 2005 [Samuel A. Rebelsky]
Wednesday, 2 February 2005 [Samuel A. Rebelsky]
Friday, 2 September 2005 [Samuel A. Rebelsky]
Tuesday, 6 September 2005 [Samuel A. Rebelsky]
Sunday, 29 January 2006 [Samuel A. Rebelsky]
This page was generated by
Siteweaver on Thu Mar 30 15:24:25 2006.
The source to the page was last modified on Tue Jan 31 11:30:23 2006.
This page may be found at