Java Reference
In-Depth Information
Although you may not realize it, you have already been using streams in your
programs when you have output something to the screen. System.out (used in
System.out.println ) is an output stream connected to the screen. System.in is an
input stream connected to the keyboard. You used System.in in expressions such as
the following:
System.out
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 System.out and 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 files are sometimes also called ASCII files because they contain
data encoded using a scheme known as ASCII coding. Files whose contents must be
handled as sequences of binary digits are called binary files .
Although it is not technically correct, you can safely think of a text file as containing
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 your
binary data files ordinarily must be read only on the same type of computer, and with
the same programming language, as the computer that created that file.
The benefit of binary files is that they are more efficient to process than text files.
Unlike other programming languages, Java also gives its binary files some of the
advantages of text files. In particular, Java binary files are platform independent; that
is, with Java, you can move your binary files from one type of computer to another
and your Java programs will still be able to read the binary files. This combines the
portability of text files with the efficiency of binary files.
The one big asset of text files is that you can read and write to them using a text editor.
With binary files, all the reading and writing must normally be done by a program.
text file
ASCII file
binary file
 
Search WWH ::




Custom Search