PROGRAMMING IN JAVA
191CSC404T
ASSIGNMENT-2
STREAMS IN JAVA
NAME: Dhanya Jayaraman
CLASS: CSE A II
REG NO: 310622104038
STREAMS IN JAVA
A stream is an abstraction that either produces or
consumes information. A stream is linked to a physical
device by the Java I/O system.
Java programs perform I/O through streams.
INPUT STREAM: Input stream can abstract many
different kinds of input, from a disk file, a keyboard, or
a network socket.
OUTPUT STREAM: Output stream may refer to the
console, a disk file, or a network connection.
To use the stream classes, you must import java.io.
The tow different types of streams are byte streams
and character streams.
BYTE STREAMS AND CHARACTER STREAMS
BYTE STREAMS: Byte streams provide a convenient
means for handling input and output of bytes.
For example, they are used for reading or writing binary
data.
CHARACTER STREAMS: character streams provide a
convenient means for handling input and output of
characters. They use Unicode.
In some cases, character streams are more efficient
than byte streams.
BYTE STREAMS CLASSES:
Byte streams are defined by using two class
hierarchies. At the top are two abstract classes:
InputStream and OutputStream.
Each of these abstract classes has several concrete
subclasses that handle the differences between various
devices, such as disk files, network connections, and
even memory buffers.
The abstract classes InputStream and
OutputStream define to most important key methods
that the other stream classes implement, that are
read() and write() which read and write bytes of data
respectively. These methods are abstract and are
overridden in derived stream classes.
CHARACTER STREAMS CLASSES:
Character streams are defined by using two class
hierarchies. At the top are two abstract classes,
Reader and Writer. These contains 2 important
methods are read() and write() which read and write
characters of data, respectively. These methods are
abstract and are overridden in derived stream classes.
THE PREDEFINED STREAMS:
The automatically imported package, java.lang, defines
a class called System. It contains three pre-defined
stream variables: in, out, err.
These fields are declared as public, static and final
within System.
System.out refers to the standard output stream. By
default, this is the console.
System.in refers to standard input, which is the
keyboard by default.
System.err refers to the standard error stream which
is also console by default.
System.in is an object of type InputStream, System.out
and System.err are objects of type PrintStream.
READING CONSOLE INPUT
Console input is accomplished by reading from
System.in. To obtain a character based stream that
is attached to the console, wrap System.in in a
BufferedReader object.
BufferedReader(Reader inputReader)
One of the concrete subclasses of BufferedReader
class is InputStreamReader.
InputStreamReader(InputStream inputstream)
The following line of code creates a
BufferedReader:
BufferedReader br=new BufferedReader (new
InputStreamReader (System.in));
WRITING CONSOLE OUTPUT
Console output is accomplished with print() and
println() defined by PrintStream class. It is derived
from OutputStream class, it also implements the
low-level method write().
PrintWriter pw=new PrintWriter (System.out, true);