Java programs perform Input Output (I/O) through the streams. A stream is an abstraction that either produces or consumes the information.
A stream is linked to a physical device by the Java I/O system.
All the streams behave in the same manner, even if the actual physical devices to which they are linked differ. Thus, the same I/O classes and the methods can be applied to the different types of devices. This means that an input stream can abstract many different kinds of input, from a keyboard, a disk file, or a network socket. Likewise, an output stream may refer to the console, a disk file, or a network connection. Streams are a clean way to deal with input/output without having every part of your code understand the difference between a keyboard and a network, for example. Java implements the streams within class hierarchies defined in the java.io package.
Types of Stream
Java defines the following two types of streams :
- Byte streams
- Character streams
Java Byte Stream
The Byte streams provide a convenient means for handling input and output of bytes. Byte streams are used, for example, when reading or writing binary data.
Byte streams are defined by using two hierarchies. At the top are the following two abstract classes :
Each of these abstract classes has several concrete subclasses that handle the differences among the various devices, such as disk files, network connections, and even memory buffers.
The byte stream classes in the java.io package are shown in the Following table :
Remember - To use the stream classes, you must import the java.io package.
|BufferedInputStream||Buffered input stream|
|BufferedOutputStream||Buffered output stream|
|ByteArrayInputStream||Input stream that reads from a byte array|
|ByteArrayOutputStream||Output stream that writes to a byte array|
|DataInputStream||An input stream that contains the methods for reading the Java standard data types|
|DataOutputStream||An output stream that contains the methods for writing the Java standard data types|
|FileInputStream||Input stream that reads from a file|
|FileOutputStream||Output stream that writes to a file|
|InputStream||Abstract class that describes stream input|
|ObjectInputStream||Input stream for objects|
|ObjectOutputStream||Output stream for objects|
|OutputStream||Abstract class that describes stream output|
|PrintStream||Output stream that contains the print() and println()|
|PushbackInputStream||Input stream that supports one-byte "unget", which returns a byte to the input stream|
|SequenceInputStream||Input stream that is a combination of two or more input streams that will be read sequentially, one after the other|
The abstract classes, InputStream and OutputStream define several key methods that the other stream classes implement. Two of the most important are read() and write() methods, respectively, which read and write bytes of data. Each has a form that is abstract and must be overridden by the derived stream classes.
Java Character Stream
The Character stream provide a convenient means for handling input and output of characters. They are Unicode and, therefore, can be internationalized. Also, in some cases, character streams are more efficient than the byte streams.
Character streams are defined by using the two class hierarchies. At the top are two abstract classes :
These abstract classes handle the Unicode character streams.
Java has several concrete subclasses of each of these. The character stream classes in the java.io package are shown in the following table :
|BufferedReader||Buffered input character stream|
|BufferedWriter||Buffered output character stream|
|CharArrayReader||Input stream that reads from a character array|
|CharArrayWriter||Output stream that writes to a character array|
|FileReader||Input stream that reads from a file|
|FileWriter||Output stream that writes to a file|
|InputStreamReader||Input stream that translates bytes to characters|
|LineNumberReader||Input stream that counts lines|
|OutputStreamWriter||Output stream that translates characters to bytes|
|PrintWriter||Output stream that contains the print() and println()|
|PushbackReader||Input stream that allows the character to be returned to the input stream|
|Reader||Abstract class that describes the character stream input|
|StringReader||Input stream that reads from a string|
|StringWriter||Output stream that writes to a string|
|Writer||Abstract class that describes the character stream output|
The abstract classes, Reader and Writer define several key methods that the other stream classes implement. Two of the most important methods are read() and write(), which read and write the characters of data, respectively. Each has a form that is abstract and must be overridden by the derived stream classes.
Java Predefined Streams
As you already know, all Java programs automatically import the java.lang package. This package defines a class called System, which encapsulates several aspects of run-time environment. For example, using some of its methods, you can obtain the current time and settings of the various properties associates with the system.
System also contains the following three predefined stream variables:
- in - System.in refers to the standard input, which is the keyboard by default
- out - System.out refers to the standard output stream. By default, this is the console
- err - System.err refers to the standard error stream, which also is the console by default.
However, these streams may be redirected to any compatible I/O devices.
These fields are declared as public, static, and final within System. This means that they can be used by any other part of your program and without reference to a specific System object.
System.in is an object of type InputStream, System.out and System.err are objects of type PrintStream. These are the byte streams, even though they are typically used to read and write the characters from and to the console. As you will see, you can wrap these within the character-based streams, if desired.
Examples on Files in Java
Here are some examples related to files in Java, you can go for.
- Read File in Java
- Write to File Java
- Read & Display File in Java
- Copy File in Java
- Merge two Files in Java
- List files in a Directory Java
- Delete File in Java
« Previous Tutorial Next Tutorial »
Follow/Like Us on Facebook
Subscribe Us on YouTube