Lesson 4 | Input streams |
Objective | Examine how input streams are used to read data. |
Input streams read Data
The InputStream
class serves as the base class for all input Stream: A stream is a medium through which data is transferred. A stream acts sort of like a pipe in the real world, except that it shuttles moving data instead of water or gas. Data is transferred through a stream one byte at a time, and can be directed in different ways. streams, and supports the following commonly used methods:
read() | reads a byte or series of bytes of data |
skip() | skips over bytes of data |
mark() | marks the current position in the input stream |
reset() | resets the stream to the position previously set by a call to mark() |
One limitation of input streams is that they read data as a series of bytes. The DataInput
interface[1] provides a means of reading data in one of Java's primitive data formats. The DataInput
interface provides methods such as readBoolean()
, readInt()
, and
readFloat()
. The DataInputStream
class implements the DataInput
interface.
The Reader
class provides an alternative to InputStream
that supports internationalization. The
Reader
class is designed similarly to InputStream
and supports a similar set of methods, so you can use both classes in a similar manner.
[1]
Interface: An interface is a set of methods that Java classes can implement.