Chapter 4. Java I/O
A large part of what network programs do is simple input and output, moving bytes from one system to another. Bytes are bytes; and to a large extent, reading data a server sends you is not all that different from reading a file. Sending text to a client is not all that different from writing a file. However, input and output (I/O) in Java is organized differently than it is in most other languages, such as C, Pascal, and C++. Consequently, I’d like to take one chapter to summarize Java’s unique approach to I/O.
I/O in Java is built on streams. Input streams read data. Output
streams write data. Different fundamental stream classes such as
java.io.FileInputStream and
sun.net.TelnetOutputStream read and write
particular sources of data. However, all fundamental output streams
have the same basic methods to write data and all fundamental input
streams use the same basic methods to read data. After a stream is
created, you can often ignore the details of exactly what it is
you’re reading or writing.
Filter streams can
be chained to either an input stream or an output stream. Filters can
modify the data as it’s read or written—for instance, by
encrypting or compressing it—or they can simply provide
additional methods for converting the data that’s read or
written into other formats. For instance, the
java.io.DataOutputStream class provides a method
that converts an int to four bytes and writes
those bytes onto its underlying output stream.
Finally, readers and writers ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access