0% found this document useful (0 votes)
19 views8 pages

Understanding Java Stream Classes

The document discusses streams in Java, which are abstractions for producing or consuming information linked to physical devices through the Java I/O system. It explains the two types of streams: byte streams for handling binary data and character streams for handling character data, along with their respective class hierarchies. Additionally, it covers predefined streams in the System class and methods for reading from and writing to the console.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views8 pages

Understanding Java Stream Classes

The document discusses streams in Java, which are abstractions for producing or consuming information linked to physical devices through the Java I/O system. It explains the two types of streams: byte streams for handling binary data and character streams for handling character data, along with their respective class hierarchies. Additionally, it covers predefined streams in the System class and methods for reading from and writing to the console.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 [Link].
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, [Link], 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.
[Link] refers to the standard output stream. By
default, this is the console.
[Link] refers to standard input, which is the
keyboard by default.
[Link] refers to the standard error stream which
is also console by default.
[Link] is an object of type InputStream, [Link]
and [Link] are objects of type PrintStream.

READING CONSOLE INPUT


 Console input is accomplished by reading from
[Link]. To obtain a character based stream that
is attached to the console, wrap [Link] 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 ([Link]));

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 ([Link], true);

Common questions

Powered by AI

PrintStream, which is used for writing console output via System.out, is a subclass of OutputStream. This relationship means that PrintStream inherits the low-level, byte-oriented write() method from OutputStream, allowing generic byte data output. However, PrintStream extends OutputStream by providing higher-level methods, such as print() and println(), specifically designed for easy text output. These methods format and convert data to strings automatically, offering developers a more straightforward way to handle output without managing bytes directly .

The preferred approach to read character-based console input in Java involves wrapping System.in in a BufferedReader object. This is done using an InputStreamReader, which converts byte streams to character streams. The BufferedReader class provides efficient reading of characters, arrays, and lines. The typical instantiation pattern involves creating a BufferedReader with the line: BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); This setup facilitates efficient and flexible reading of text input from the console .

The class hierarchy for byte streams in Java is defined by the top-level abstract classes InputStream and OutputStream. These classes provide the fundamental byte data read() and write() methods which their subclasses, such as FileInputStream and FileOutputStream, must implement. Character streams are defined by the abstract classes Reader and Writer. These classes handle characters through their read() and write() methods. Their subclasses, such as FileReader and FileWriter, tailor the generic read and write operations for specific character data sources and sinks .

Wrapping System.in in a BufferedReader serves to enhance the efficiency of character input operations. BufferedReader allows for buffered input, meaning it reads data into a buffer as it arrives, which can reduce the number of I/O operations needed, thereby improving performance. This setup also provides convenient methods such as readLine() for reading a line of text, simplifying the process of character input from the console. The efficiency gains make it an optimal choice for applications requiring frequent or large volume console input .

InputStream and OutputStream are the abstract classes that form the basis of byte streams in Java. They define the essential methods such as read() and write(), which their subclasses must implement to handle byte data for various I/O tasks. Subclasses are tailored to handle specific device communications, like disk files or network connections, by overriding these methods to provide the stream functionality for bytes .

System.in, System.out, and System.err are predefined stream objects in the System class used for standard input, standard output, and standard error stream, respectively. System.in is typically used for reading input from the keyboard, and it is of type InputStream. System.out, used for writing output to the console, is of type PrintStream and allows usage of print() and println() methods. System.err is used for error messages. Their existence allows developers to perform basic I/O operations efficiently, wrapping System.in in a BufferedReader for character input, and using PrintWriter for outputting to the console with System.out .

Byte streams handle input and output of raw byte data, which makes them suitable for reading and writing binary data. They are defined using the classes InputStream and OutputStream and their subclasses. Character streams, on the other hand, handle input and output of characters and use Unicode, making them more efficient for text data. Character streams are defined using the Reader and Writer classes. The choice between these two types depends on the nature of the data; byte streams are used for binary data, while character streams are preferred for text data due to their efficiency in handling characters .

System.in, System.out, and System.err inherit from specific Java classes which affect their functionality. System.in is an InputStream, enabling it to handle byte input. System.out and System.err, both being PrintStream objects, allow formatted output and use low-level write() methods to output bytes, but also provide high-level print() and println() for easier text output. This abstract handling through inheritance provides rich I/O functionality and reinforces a layered abstraction, where underlying byte-oriented operations can support higher-level, more user-friendly interfaces .

In byte stream classes derived from InputStream and OutputStream, read() and write() methods handle raw bytes, making them ideal for binary data transfer. In contrast, character stream classes derived from Reader and Writer handle characters, more suitable for handling text data due to their Unicode support. The different handling implies that byte streams would be chosen for any data that is not inherently text, such as images or binary files, whereas character streams are efficient in parsing and processing textual data like strings and files with text content .

Character streams in Java are designed to handle text using Unicode, which allows them to represent a wide range of characters from various writing systems globally. This choice facilitates Java's ability to support internationalization, as Unicode can encode virtually all characters used in the world's languages. Consequently, programs using character streams are capable of processing internationalized text data without the limitations posed by traditional character encodings that might only support specific languages or regions. This inclusion broadens Java's applicability in global software development, ensuring consistency and correctness in character handling across diverse linguistic contexts .

You might also like