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

JavaIOStreams 5 Revised

Java I/O streams facilitate reading from and writing to data sources, categorized into byte-based (InputStream, OutputStream) and character-based (Reader, Writer) streams. Combining streams, such as using BufferedInputStream and BufferedOutputStream, enhances performance through buffering and batching. The document also discusses the integration of Readers and Writers with InputStreams and OutputStreams for efficient text processing.

Uploaded by

lulumondijimon
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

JavaIOStreams 5 Revised

Java I/O streams facilitate reading from and writing to data sources, categorized into byte-based (InputStream, OutputStream) and character-based (Reader, Writer) streams. Combining streams, such as using BufferedInputStream and BufferedOutputStream, enhances performance through buffering and batching. The document also discusses the integration of Readers and Writers with InputStreams and OutputStreams for efficient text processing.

Uploaded by

lulumondijimon
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java I/O Streams


Java I/O streams are flows of data you can either read from, or write to.

streams are typically connected to a data source, or data destination, like a
file or a network connection.

Java IO streams are typically either byte-based or character-based.

The streams that are byte based are typically called something with "stream",
like InputStream or OutputStream.

These streams read and write a raw byte at a time, with the exception of the
DataInputStream and DataOutputStream which can also read and write int,
long, float and double.

The streams that are character-based are typically called something with
"Reader" or "Writer“.

The character based streams can read / write characters.
InputStream class

The class [Link] is the base class for all Java byte based IO
input streams.

You typically read data from an InputStream by calling the read()
method.

The read() method returns an int containing the byte value of the byte
read.

If there is no more data to be read, read() method typically returns -1.
OutputStream class
 The class [Link] is the base class for all Java byte based IO
output streams.
 You typically write data to an OutputStream by calling the write() method.
 Try to make sure that component depends on an OutputStream and not one
of its subclasses.
 Here is a simple example pushing some data out to a file:
Combining Streams

 You can combine streams into chains to achieve more advanced input
and output operations. For instance, reading every byte one at a time
from a file is slow. It is faster to read a larger block of data from the disk
and then iterate through that block byte for byte afterwards.
 To achieve buffering you can wrap your InputStream in an
BufferedInputStream.

 Buffering can also be applied to OutputStream thereby batching the


writes to disk (or the underlying stream) up in larger chunks. That provides
faster output too. This is done with a BufferedOutputStream.
 Buffering is just one of the effects you can achieve by combining streams.
Java Stream Combinations and their Effects

Purpose Stream Example What It Adds

Buffering BufferedInputStream, BufferedOutputStream Batches reads/writes

Data conversion DataInputStream, DataOutputStream Reads/writes Java primitives

Character encoding InputStreamReader, OutputStreamWriter Converts bytes ↔ characters

Compression GZIPInputStream, GZIPOutputStream Compresses/decompresses data

Encryption CipherInputStream, CipherOutputStream Adds encryption/decryption


Java IO: Readers & Writers

The Java Reader ([Link]) and Java Writer class ([Link]) in Java IO work
much like the InputStream and OutputStream with the exception
that Reader and Writer are character based. They are intended for reading and writing
text. The InputStream and OutputStream are byte based, remember


The Java Reader is the base class of all Reader's in the Java IO API. Subclasses include
a BufferedReader, PushbackReader, InputStreamReader, StringReader and several others.
Java IO: Readers

Notice, that while an InputStream returns one byte at a time, meaning a value between 0 and
255 (or -1 if the stream has no more data), the Reader returns a char at a time, meaning a
value between 0 and 65535 (or -1 if the stream has no more data). This does not necessarily
mean that the Reader reads two bytes at a time from the source it is connected to. It may
read one or more bytes at a time, depending on the encoding of the text being read.

Combining Readers with InputStreams:

Java Reader can be combined with an InputStream. If you have an InputStream and want to
read characters from it, you can wrap it in an InputStreamReader. Pass the InputStream to
the constructor of the InputStreamReader like this:


In the constructor you can also specify what character set to use to decode the text
Java IO: Writers
 The Java Writer class is the base class of all Writers in the Java IO API. Subclasses
include BufferedWriter and PrintWriter among others.
 Here is a simple Java IO Writer example:

 Combining Writers With OutputStreams


 A Java Writer can be combined with an OutputStream just like Readers and InputStream's.
Wrap the OutputStream in an OutputStreamWriter and all characters written to
the Writer are passed on to the OutputStream.

You might also like