Java Reference
In-Depth Information
10
As a leaf is carried by a stream, whether the stream ends in a lake or in
the sea, so too is the output of your program carried by a stream, not
knowing if the stream goes to the screen or to a file.
WASHROOM WALL OF A COMPUTER SCIENCE DEPARTMENT (1995)
Introduction
In this chapter, we explain how you can write your programs to take input from a file
and send output to a file. This chapter covers the most common ways of doing file I/O
in Java. However, it is not an exhaustive study of Java I/O classes. The Java I/O class
library contains bewilderingly many classes and an exhaustive treatment of all of them
would be a book by itself.
Prerequisites
You need only some of Chapter 9 on exception handling to read this chapter. You do
not need Chapters 6 , 7 , or 8 on arrays, inheritance, and polymorphism, except in the
final subsection, which covers writing and reading of arrays to binary files. If you have
not yet covered some basic material on one-dimensional arrays, you can, of course,
simply omit this last subsection.
You may postpone all or part of this chapter if you wish. Nothing in the rest of this
book requires any of this chapter.
10.1
Introduction to File I/O
Good Heavens! For more than forty years I have been speaking prose without
knowing it.
MOLIÈRE, LE Bourgeois Gentilhomme
In this section, we go over some basic concepts about file I/O before we go into any
Java details.
Streams
A stream is an object that allows for the flow of data between your program and some
I/O device or some file. If the flow is into your program, the stream is called an input
stream . If the flow is out of your program, the stream is called an output stream . If
the input stream flows from the keyboard, then your program will take input from the
keyboard. If the input stream flows from a file, then your program will take its input
from that file. Similarly, an output stream can go to the screen or to a file.
stream
input stream
output stream
 
 
Search WWH ::




Custom Search