Java I/O
What a long strange trip this book has been.
When I began writing
the first draft back in September, 1997, I thought it would take me
about two months and 200 pages. Now, a year and a half and 600 pages later,
I've learned that I/O is a much bigger, more interesting, and more important
topic than I originally dreamed. But I have managed to create
what is unquestionably the best book I've
ever written, and one in which I expect that all Java programmers will find something
worthwhile, so I hope you won't mind if I take a few minutes
to tell you about this book.
Java's input and output libraries have been unfairly maligned
for some time. It is widely (but incorrectly) believed that
I/O in Java is a lot weaker and a lot harder than
I/O in traditional languages like C, C++, and Pascal.
If anything, however, the exact opposite is true.
Java's I/O tools are far more
sophisticated, more powerful, and easier to use than
traditional printf()
/scanf()
style I/O.
Java's input and output (I/O) is based on streams. Streams
are a convenient metaphor for reading and writing data regardless
of whether that data comes from a file, a network connection,
the console, another thread, or some other source.
Furthermore streams can be filtered to perform
encryption, serialization, compression, translation,
and other services.
Java I/O tells you all you need to know about the four
main categories of streams and uncovers less known features to
help make your I/O operations more efficient. Plus, it shows you
how to control number formatting, use characters aside from
the standard ASCII character set, and get a head start on writing
truly multilingual software.
I/O
in C, C++, and most other languages, is hamstrung by the
assumption that what's being read or written is a 1970s era dumb
terminal, or at least something very much like it. Java is the
first major language to throw out this assumption. The designers
of Java recognized that programs that read and write files, network
connections, and serial port devices are a lot more important
than toy programs from CS101 that read a number from the command
line and square it. Consequently they created a language that focuses on
solving professional programmers' real problems, instead of
CS101 textbook examples. Rather than copying old-fashioned I/O APIs based on
dumb ASCII terminals from the 1970s or DOS prompts from the 1980s,
Java's designers created a new I/O API based on abstract streams. This API
better suits the international network programs of the 1990s.
Unfortunately, precisely because Java I/O is structured so
differently from I/O in the languages most of us grew up with,
many programmers don't realize how simple and powerful it really
is. I first noticed this after I wrote Java
Network Programming. Many of the questions I got from
readers weren't about network programming at all.
They were about I/O. That's when I began thinking
seriously about writing a prequel to Java Network Programming; a book
that would cover the java.io
package
and show programmers how to really take advantage of
Java's I/O facilities to write very powerful programs
far more quickly than they could in any other language.
Java I/O is that book.
Here are just a few of the topics covered in depth in Java I/O:
- How to take advantage of the polymorphic nature of
InputStream
and OutputStream
- Files and network connections, the two most common sources of input and output
- Filter streams
- Reading and writing text
- Padding and aligning numbers
- Communicating with serial and parallel port devices
- The Java Cryptography Extension
- Object
Serialization
- Data Compression
- Writing file access code that works across platforms
- File dialogs in both the classic AWT and Swing
And that's really just the beginning of what you'll learn from
Java I/O. I'm really happy with this book.
In fact it's perhaps the first book I've written that I really feel reached its full potential.
I do hope you'll check it out. It's available
from
amazon.com,
Fat Brain,
and other purveyors of fine computer books.
If you need to special order it, the ISBN number is 1-56592-485-1.
It's $32.95, 596 pages, published by O'Reilly, and written by me, Elliotte
Rusty Harold.
- Preface
- Part I: Basic I/O
-
1. Introducing I/O
- What Is a Stream?
- Numeric Data
- Character Data
- Readers and Writers
- The Ubiquitous IOException
- The Console: System.out, System.in, and System.err
- Security Checks on I/O
2. Output Streams
- The OutputStream Class
- Writing Bytes to Output Streams
- Writing Arrays of Bytes
- Flushing and Closing Output Streams
- Subclassing OutputStream
- A Graphical User Interface for Output Streams
3. Input Streams
- The InputStream Class
- The read( ) Method
- Reading Chunks of Data from a Stream
- Counting the Available Bytes
- Skipping Bytes
- Closing Input Streams
- Marking and Resetting
- Subclassing InputStream
- An Efficient Stream Copier
- Part II: Data Sources
-
4. File Streams
- Reading Files
- Writing Files
- File Viewer, Part 1
5. Network Streams
- URLs
- URL Connections
- Sockets
- Server Sockets
- URLViewer
- Part III: Filter Streams
-
6. Filter Streams
- The Filter Stream Classes
- The Filter Stream Subclasses
- Buffered Streams
- PushbackInputStream
- Print Streams
- Multitarget Output Streams
- File Viewer, Part 2
7. Data Streams
- The Data Stream Classes
- Reading and Writing Integers
- Reading and Writing Floating-Point Numbers
- Reading and Writing Booleans
- Reading Byte Arrays
- Reading and Writing Text
- Miscellaneous Methods
- Reading and Writing Little-Endian Numbers
- Thread Safety
- File Viewer, Part 3
8. Streams in Memory
- Sequence Input Streams
- Byte Array Streams
- Communicating Between Threads with Piped Streams
9. Compressing Streams
- Inflaters and Deflaters
- Compressing and Decompressing Streams
- Working with Zip Files
- Checksums
- JAR Files
- File Viewer, Part 4
10. Cryptographic Streams
- Hash Function Basics
- The MessageDigest Class
- Digest Streams
- Encryption Basics
- The Cipher Class
- Cipher Streams
- File Viewer, Part 5
- IV. Advanced and Miscellaneous Topics
-
11. Object Serialization
- Reading and Writing Objects
- Object Streams
- How Object Serialization Works
- Performance
- The Serializable Interface
- The ObjectInput and ObjectOutput Interfaces
- Versioning
- Customizing the Serialization Format
- Resolving Classes
- Resolving Objects
- Validation
- Sealed Objects
12. Working with Files
- Understanding Files
- Directories and Paths
- The File Class
- Filename Filters
- File Filters
- File Descriptors
- Random-Access Files
- General Techniques for Cross-Platform File Access Code
13. File Dialogs and Choosers
- File Dialogs
- JFileChooser
- File Viewer, Part 6
14. Multilingual Character Sets and Unicode
- Unicode
- Displaying Unicode Text
- Unicode Escapes
- UTF-8
- The char Data Type
- Other Encodings
- Converting Between Byte Arrays and Strings
15. Readers and Writers
- The java.io.Writer Class
- The OutputStreamWriter Class
- The java.io.Reader Class
- The InputStreamReader Class
- Character Array Readers and Writers
- String Readers and Writers
- Reading and Writing Files
- Buffered Readers and Writers
- Print Writers
- Piped Readers and Writers
- Filtered Readers and Writers
- File Viewer Finis
16. Formatted I/O with java.text
- The Old Way
- Choosing a Locale
- Number Formats
- Specifying Width with FieldPosition
- Parsing Input
- Decimal Formats
- An Exponential Number Format
17. The Java Communications API
- The Architecture of the Java Communications API
- Identifying Ports
- Communicating with a Device on a Port
- Serial Ports
- Parallel Ports
- Part V: Appendixes
-
A. Additional Resources
B. Character Sets
Copyright 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified April 9, 1999