Input and Output Streams
Site: | Saylor Academy |
Course: | CS101: Introduction to Computer Science I |
Book: | Input and Output Streams |
Printed by: | Guest user |
Date: | Wednesday, May 14, 2025, 1:47 AM |
Description
This chapter explains how input and output streams can be used for writing files, similar to how they are used for writing to the display.
1. Input and Output Streams
This chapter discusses input and output (I/O) using the package java.io
. I/O is done using streams. A stream is a sequence of data flowing from a source to a destination. I/O streams can be connected to a wide variety of
data sources and destinations.
You have already used streams in reading data from the keyboard and in writing data to the monitor. Streams are used for much more than that.
Chapter Topics:
- I/O Streams
- Readers and Writers
- InputStreams and OutputStreams
- Character data and UTF
- Processing Streams
Question 1:
Java can read data from the Internet. What does this data look like to a Java program?
Source: Bradley Kjell, https://chortle.ccsu.edu/Java5/Notes/chap82/ch82_1.html This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 License.
2. I/O Streams
Answer:
Internet data looks like an I/O stream.
I/O Streams
The core Java language does not have any I/O methods. For a program to do I/O, it must import
an I/O package. These notes (and most programs) use the package java.io
.
Data for a program may come from several sources. Data created by a program may be sent to several destinations. The connection between a program and a data source or destination is called a stream.
An input stream handles data flowing into a program. An output stream handles data flowing out of a program.
In the picture, each "O" is a piece of data. The data are streaming from the source into the program. Computed results are streaming from the program to the destination.
Question 2:
Could a disk file be an input stream?
3. Types of Sources and Destinations
Answer:
Yes. (It could also be an output stream.)
Types of Sources and Destinations
There are many types of I/O devices. Some can be a source, others can be a destination, and others can be both source and destination. Often the destination for one stream is the source for another. In the table, decide what role each object can play.
Some objects switch roles depending on what program is running. For example a disk file might be the destination for the output of one program, and later it may be the source for another program.
Question 3:
Do you think it is possible for a program to use several input and several output streams?
4. Processing Streams
Answer:
Yes. For example, a text editor typically does output to both the monitor and to a disk file. Its input comes from both a disk file and the keyboard.
Processing Streams
A processing stream operates on the data supplied by another stream. Often a processing stream acts as a buffer for the data coming from another stream. A buffer is a block of main memory used as a work area. For example, disks usually deliver data in blocks of 512 bytes, no matter how few bytes a program has asked for. Usually the blocks of data are buffered and delivered from the buffer to the program in the amount the program asked for.
In the picture, the keyboard sends data to the InputStream System.in
which is connected to a InputStreamReader stream which is connected to a BufferedReader stream. System.in
is a stream object that the Java system automatically creates when your program starts running.
The data is transformed along the way. The raw bytes from the keyboard are grouped together into a String
object that the program reads using stdin.readLine()
.
A program can set all this up by declaring a BufferedReader
as follows:
BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );
This may seem like an unnecessary complication. But java.io
gives you a collection of parts that can be assembled to do nearly any I/O task you need.
Question 4:
(Thought Question: ) Might several connected streams be used for output?
5. Character Streams and Byte Streams
Answer:
Yes. For example, buffering is useful for output as well as input.
Character Streams and Byte Streams
Character streams are intended exclusively for character data.
Byte streams are intended for general purpose input and output.
Of course, fundamentally all data consists of patterns of bits grouped into 8-bit bytes. So, logically all streams could be called "byte streams". However streams that are intended for bytes that represent characters are called "character streams" and all others are called "byte streams".
Character streams are optimized for character data and perform some other useful character-oriented tasks (more on this later). Often the source or destination of a character stream is a text file, a file that contains bytes that represent characters.
So far in these chapters, programs have done I/O only with character data (usually with the keyboard as the source and the monitor as the destination). However, data sources and destinations often contain non-character data. For example, the bytecode file created by the Java compiler contains machine instructions for the Java virtual machine. These are not intended to represent characters, and input and output of them must use byte streams.
Question 5:
Think of another type of file that contains data other than characters.
6. Types of Streams
Answer:
Image files, audio files, executable files from other programming languages, many kinds of data files, ... the list is endless.
Types of Streams
A stream object may be:
- An input stream or an output stream,
- a processing stream or an ordinary stream,
- a character-oriented stream or a byte-oriented stream,
- and may be connected to a variety of sources or destinations.
There are many types of streams. For example, a stream might be an "input, character-oriented, processing stream". Another stream might be an "byte-oriented output stream". Not only are there many types of streams, there are many ways to connect them together. The I/O aspects of a program can get complicated.
This situation is common to all industrial-strength programming languages. I/O is a big topic because of the wide variety of IO devices and the wide variety of data formats. A production language must be able to read data from sources written by any language running on any type of computer.
For example, a program might need to read data from magnetic tapes written 30 years ago by a COBOL program running on an IBM mainframe. (You would likely use byte-oriented streams for this).
Fortunately, if a program is written in Java it will use identical data formats on all computers. If a Java program running on a Macintosh writes a text file, that file can easily be read by a Java program running on a PC or on any other computer.
This is not true of most other languages. A C program running on one type of computer is unlikely to create a file that can easily be read by a C program running on another type of computer. Worse than that, two different C compilers for the same computer might use different data formats!
Question 6:
A C++ program running on a IBM mainframe has written a file of integer data. Do you think that a C++ program running on a PC can read this file without trouble?
7. I/O Class Hierarchy
Answer:
No.
Of course, C++ is an industrial-strength language, so you would be able to write a C++ program that reads the file. But it would be a lot of trouble to do so.
I/O Class Hierarchy
The diagram shows the top of the hierarchy for the java.io
package. The dotted clouds are abstract classes. They act as base classes for specialized streams (to be discussed shortly).
Streams are byte-oriented or character-oriented. Each type has input streams and output streams.
- Byte-oriented streams.
- Intended for general-purpose input and output.
- Data may be primitive data types or raw bytes.
- Character-oriented streams.
- Intended for character data.
- Data is transformed from/to 16 bit Java
char
used inside programs to the UTF format used externally.
In the diagram, abstract classes are drawn as clouds with a dotted border.
Question 7:
Is a
Reader
an input stream or an output stream?
8. Readers and Writers
Answer:
A Reader
is an input stream.
Readers and Writers
Reader
s and Writer
s deal with character streams. These are abstract classes. A program must use classes derived from them. For example, a BufferedReader
is a Reader
.
Character streams are optimized for handling character data. They also translate between the internal format used by Java programs and an external format used for text files. Inside a Java program character data is represented with the 16-bit char
data type. The characters of a String
also use this 16-bit code. On a disk file, characters are represented in a format called UTF. This format uses one to four bytes per character and is intended to be a universal format—one format for all text files in any language anywhere in the world.
UTF stands for "Unicode Transformation Format". Usually a UTF text file is identical to an ASCII text file. ASCII is the standard way to represent characters that most computers have used for the past forty years. A file created with a text editor is usually an ASCII text file.
A UTF text file can include non-ASCII characters such as Cyrillic, Greek, and Asian characters. By reading and writing UTF files, Java programs can process text from any of the World's languages.
Question 8:
Could a Java program use the alphabet used on the island of Java?
9. Readers
Answer:
Yes. (Seems only fair.)
Readers
Reader
s and Writer
s deal with character streams. These are abstract classes. A program must use classes derived from them. For example, a BufferedReader
is a Reader
.
Character streams are optimized for handling character data. They also translate between the internal format used by Java programs and an external format used for text files. Inside a Java program character
data is represented with the 16-bit char
data type. The characters of a String
also use this 16-bit code. On a disk file, characters are represented in a format called UTF. This format uses
one to four bytes per character and is intended to be a universal format—one format for all text files in any language anywhere in the world.
UTF stands for "Unicode Transformation Format". Usually a UTF text file is identical to an ASCII text file. ASCII is the standard way to represent characters that most computers have used for the past forty years. A file created with a text editor is usually an ASCII text file.
A UTF text file can include non-ASCII characters such as Cyrillic, Greek, and Asian characters. By reading and writing UTF files, Java programs can process text from any of the World's languages.
Question 9:
What (do you think) is the source of data for a
FileReader
?
10. Writer
Answer:
A file.
Writer
Class FileReader
is discussed in the following chapters. But now let us look at the classes that descend from Writer
, an abstract class from which all character-oriented output streams are derived. All these streams are aimed at receiving 16-bit char
data from a program, and sending it to another destination, which may use a different character format (such as UTF format on a disk file).
All these classes are character-oriented output streams. We will be especially interested in FileWriter
and PrintWriter
classes.
Question 10:
(Review: ) What type of data does an
InputStream
handle?
11. InputStream
Answer:
An InputStream
handles byte data.
InputStream
InputStream
is an abstract class from which all byte-oriented input streams are derived. Its descendant classes are used for general-purpose input (non-character input). These streams deliver data to a program in groups of 8-bit bytes. The bytes can be grouped into the size necessary for the type of data.
For example, if a disk file contains 32-bit int
data, data can be delivered to the program in 4-byte groups in the same format as Java primitive type int
.
We will be mostly concerned with DataInputStream
and FileInputStream
.
Question 11:
(Review: ) What type of data does an
OutputStream
handle?
12. OutputStream
Answer:
An OutputStream
handles byte data.
OutputStream
OutputStream is an abstract class from which all byte-oriented output streams are derived. Its descendant classes are used for general-purpose (non-character output). These streams are aimed at writing groups of 8-bit bytes to output destinations. The bytes are in the same format as Java primitive types. For example, 4-byte groups corresponding to type int
can be written to a disk file.
We will mostly be interested in FileOutputStream
and DataOutputStream
. We have used PrintStream
many times already, because System.out
is an object of that type.
Question 12:
Say that you wrote 32-bit
int
data to a disk file usingDataOutputStream
. What input stream class would another program use to read that file?
13. Object Streams
Answer:
DataInputStream
Object Streams
You may have noticed the class ObjectOutputStream
in the previous diagram. Streams of that class write objects to a destination such as a disk file. ObjectInputStream
streams read objects from a source. So far in our programs objects have existed only in main memory and lasted only as long the program that created them was running. When the program stopped running (or earlier), they were garbage collected.
With an ObjectOutputStream
an object can be written to disk and will remain there even after the program stops. This is the topic of object serialization, an advanced topic not covered in these notes.
Question 13:
(Thought question: ) Could an object created by a program running on a Macintosh computer be transferred to a program running on a PC?
14. End of the Chapter
Answer:
Yes. Java uses the same formats for data, even object data, on all systems.
End of the Chapter
- I/O Streams.
- Data Sources and destinations.
- Processing streams.
- Character streams.
- Types of streams.
- Top level of the I/O class hierarchy.
- UTF character format.
- Readers.
- Writers.
- Class InputStream
- Class OutputStream