Java Reference
In-Depth Information
Streams
stream
input stream
output
stream
A
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
stream
input
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.
Although you may not realize it, you have already been using streams in your programs.
The
. If the flow is out of your program, the stream is called an
output stream
that you have already used (in such things as
) is an
System.out
System.in
System.out
System.out.println
output stream connected to the screen.
is an input stream connected to the key-
System.in
board. You used
in expressions like the following:
System.in
Scanner keyboard = new Scanner(System.in);
These two streams are automatically available to your program. You can define other
streams that come from or go to files. Once you have defined them, you can use them in
your program in ways that are similar to how you use
and
.
System.out
System.in
Streams
A
stream
is a flow of data. If the data flows
into your program,
then the stream is called an
input stream
. If the data flows
out of your program,
the stream is called an
output stream
.
Streams are used for both console I/O, which you have been using already, and file I/O.
Text Files and Binary Files
Text files are files that appear to contain sequences of characters when viewed in a text
editor or read by a program. For example, the files that contain your Java programs are
text files.
text file
ASCII file
binary file
because they contain data
encoded using a scheme known as ASCII coding. Files whose contents must be han-
dled as sequences of binary digits are called
Text files
are sometimes also called
ASCII files
.
Although it is not strictly speaking correct, you can safely think of a text file as con-
taining a sequence of characters, and think of a binary file as containing a sequence of
binary digits. Another way to distinguish between binary files and text files is to note
that text files are designed to be read by human beings, whereas binary files are
designed to be read only by programs.
One advantage of text files is that they are usually the same on all computers, so
you can move your text files from one computer to another with few or no problems.
The implementation of binary files usually differs from one computer to another, so
binary files
Search WWH ::




Custom Search