Output Streams Section 2.1.. Writing Bytes to Output Streams Section 2.2.. Flushing Output Streams Section 2.5.. Input Streams Section 3.1... Multitarget Output Streams Section 6.7..
Trang 2Java™ I/O, 2nd Edition
Table of Contents
Copyright
Preface
Part PART I: Basic I/O
Chapter 1 Introducing I/O Section 1.1 What Is a Stream? Section 1.2 Numeric Data Section 1.3 Character Data Section 1.4 Readers and
Writers
Section 1.5 Buffers and
Channels
Trang 3Section 1.6 The Ubiquitous IOException
Section 1.7 The Console:
System.out, System.in, and
System.err
Section 1.8 Security Checks on I/O
Chapter 2 Output Streams
Section 2.1 Writing Bytes to Output Streams
Section 2.2 Writing Arrays of Bytes
Section 2.3 Closing Output
Streams
Trang 4Section 2.4 Flushing Output Streams
Section 2.5 Subclassing
OutputStream
Section 2.6 A Graphical User Interface for Output Streams Chapter 3 Input Streams
Section 3.1 The read( )
Trang 5Section 3.5 Closing Input
Section 4.1 Reading Files
Section 4.2 Writing Files
Section 4.3 File Viewer, Part 1 Chapter 5 Network Streams
Trang 7Section 6.5.
ProgressMonitorInputStream Section 6.6 Multitarget Output Streams
Section 6.7 File Viewer, Part 2 Chapter 7 Print Streams
Section 7.1 Print Versus Write Section 7.2 Line Breaks
Section 7.3 Error Handling Section 7.4 printf( )
Section 7.5 Formatter
Section 7.6 Formattable
Chapter 8 Data Streams
Section 8.1 The Data Stream
Trang 8Section 8.5 Byte Arrays
Section 8.6 Strings and chars Section 8.7 Little-Endian
Numbers
Section 8.8 Thread Safety Section 8.9 File Viewer, Part 3 Chapter 9 Streams in Memory Section 9.1 Sequence Input Streams
Trang 9Section 9.2 Byte Array
Streams
Section 9.3 Communicating Between Threads Using Piped Streams
Trang 104
Chapter 11 JAR Archives
Section 11.1 Meta-Information: Manifest Files and Signatures Section 11.2 The jar Tool
Section 11.3 The java.util.jar Package
Trang 13Section 13.9 Resolving Objects Section 13.10 Validation
Section 13.11 Sealed Objects Section 13.12 JavaDoc
Part PART IV: New I/O
Section 14.6 Mark and Reset
Trang 14Section 14.7 Compaction Section 14.8 Duplication Section 14.9 Slicing
Section 14.10 Typed Data Section 14.11 Read-Only Buffers
Section 14.12 CharBuffers Section 14.13 Memory- Mapped I/O
Trang 15Between Streams and Channels Section 15.4 Socket Channels Section 15.5 Server Socket Channels
Section 15.6 Datagram
Channels
Chapter 16 Nonblocking I/O Section 16.1 Nonblocking I/O Section 16.2 Selectable
Channels
Section 16.3 Selectors
Section 16.4 Selection Keys Section 16.5 Pipe Channels Part PART V: The File System
Trang 16Chapter 17 Working with Files Section 17.1 Understanding Files
Section 17.2 Directories and Paths
Section 17.3 The File Class Section 17.4 Filename Filters Section 17.5 File Filters
Section 17.6 File Descriptors Section 17.7 Random-Access Files
Section 17.8 General
Techniques for Cross-Platform File Access Code
Trang 17Chapter 18 File Dialogs and Choosers
Section 18.1 File Dialogs
Section 18.2 JFileChooser Section 18.3 File Viewer, Part
6
Part PART VI: Text
Chapter 19 Character Sets and Unicode
Section 19.1 The Unicode
Trang 19Section 20.12 Filtered Readers and Writers
Trang 20Section 20.13 File Viewer Finis Chapter 21 Formatted I/O with java.text
Section 21.1 The Old Way Section 21.2 Choosing a
Locale
Section 21.3 Number Formats Section 21.4 Specifying Width with FieldPosition
Section 21.5 Parsing Input Section 21.6 Decimal Formats Part PART VII: Devices
Chapter 22 The Java
Communications API
Trang 21Section 22.1 The Architecture
of the Java Communications API
Section 22.2 Identifying Ports Section 22.3 Communicating with a Device on a Port
Section 22.4 Serial Ports
Section 22.5 Parallel Ports Chapter 23 USB
Section 23.1 USB Architecture Section 23.2 Finding Devices Section 23.3 Controlling
Devices
Section 23.4 Describing
Trang 22Section 24.1 The Generic
Trang 23Section 24.5 Serial I/O
Trang 24Section 25.7 Talking to Devices
Part PART VIII: Appendix Character Sets
About the Author
Trang 26V
W Z
Trang 27Java™ I/O, 2nd Edition
By Elliotte Rusty Harold
Table of Contents | Index
All of Java's Input/Output (I/O) facilities are based on
streams, which provide simple ways to read and write
data of different
types Java provides many different kinds of streams,
each with its own
Trang 28application The universe of streams is divided into four large
categories: input streams and output streams, for reading and writing
binary data; and readers and writers, for reading and writing textual
(character) data You're almost certainly familiar with the basic kinds
of streams but did you know that there's a
to make your I/O operations more efficient? Java I/O,
2nd Edition has been updated for Java 5.0 APIs and tells you
all you ever need to know about streams and probably more
A discussion of I/O wouldn't be complete without treatment of character
sets and formatting Java supports the UNICODE standard, which provides
Trang 29definitions for the character sets of most written
for formatting textual and numeric data Java I/O,
2nd Edition shows you how to control number
formatting, use characters aside from
the standard (but outdated) ASCII character set, and get
a head start
on writing truly multilingual software
Java I/O, 2nd Edition includes:
Coverage of all I/O classes and related classes In-depth coverage of Java's number formatting facilities
and its support for International character sets
Trang 31Java™ I/O, 2nd Edition
By Elliotte Rusty Harold
Part PART I: Basic I/O
Chapter 1 Introducing I/O
Trang 32Section 1.1 What Is a Stream?
Section 1.2 Numeric Data
Section 1.3 Character Data
Section 1.4 Readers and Writers
Section 1.5 Buffers and Channels
Section 1.6 The Ubiquitous IOException
Section 1.7 The Console: System.out,
System.in, and System.err
Section 1.8 Security Checks on I/O
Chapter 2 Output Streams
Section 2.1 Writing Bytes to Output
Streams
Section 2.2 Writing Arrays of Bytes
Section 2.3 Closing Output Streams
Section 2.4 Flushing Output Streams
Section 2.5 Subclassing OutputStream
Section 2.6 A Graphical User Interface
for Output Streams
Chapter 3 Input Streams
Section 3.1 The read( ) Method
Section 3.2 Reading Chunks of Data
Trang 33from a Stream
Section 3.3 Counting the Available Bytes
Section 3.4 Skipping Bytes
Section 3.5 Closing Input Streams
Section 3.6 Marking and Resetting
Section 3.7 Subclassing InputStream
Section 3.8 An Efficient Stream Copier
Part PART II: Data Sources
Chapter 4 File Streams
Section 4.1 Reading Files
Section 4.2 Writing Files
Section 4.3 File Viewer, Part 1
Chapter 5 Network Streams
Part PART III: Filter Streams
Chapter 6 Filter Streams
Trang 34Section 6.1 The Filter Stream Classes
Section 6.2 The Filter Stream Subclasses
Section 6.3 Buffered Streams
Section 6.4 PushbackInputStream
Section 6.5 ProgressMonitorInputStream
Section 6.6 Multitarget Output Streams
Section 6.7 File Viewer, Part 2
Chapter 7 Print Streams
Section 7.1 Print Versus Write
Section 7.2 Line Breaks
Section 7.3 Error Handling
Section 7.4 printf( )
Section 7.5 Formatter
Section 7.6 Formattable
Chapter 8 Data Streams
Section 8.1 The Data Stream Classes
Section 8.2 Integers
Section 8.3 Floating-Point Numbers
Section 8.4 Booleans
Trang 35Section 8.5 Byte Arrays
Section 8.6 Strings and chars
Section 8.7 Little-Endian Numbers
Section 8.8 Thread Safety
Section 8.9 File Viewer, Part 3
Chapter 9 Streams in Memory
Section 9.1 Sequence Input Streams
Section 9.2 Byte Array Streams
Section 9.3 Communicating Between
Threads Using Piped Streams
Chapter 10 Compressing Streams
Section 10.1 Inflaters and Deflaters
Section 10.2 Compressing and
Decompressing Streams
Section 10.3 Zip Files
Section 10.4 Checksums
Section 10.5 File Viewer, Part 4
Chapter 11 JAR Archives
Section 11.1 Meta-Information: Manifest
Files and Signatures
Section 11.2 The jar Tool
Trang 36Section 11.3 The java.util.jar Package
Chapter 12 Cryptographic Streams
Section 12.1 Hash Functions
Section 12.2 The MessageDigest Class
Section 12.3 Digest Streams
Section 12.4 Encryption Basics
Section 12.5 The Cipher Class
Section 12.6 Cipher Streams
Section 12.7 File Viewer, Part 5
Chapter 13 Object Serialization
Trang 37Section 13.1 Reading and Writing
Objects
Section 13.2 Object Streams
Section 13.3 How Object Serialization
Section 13.8 Resolving Classes
Section 13.9 Resolving Objects
Section 14.1 Copying Files with Buffers
Section 14.2 Creating Buffers
Section 14.3 Buffer Layout
Trang 38Section 14.4 Bulk Put and Get
Section 14.5 Absolute Put and Get
Section 14.6 Mark and Reset
Section 14.7 Compaction
Section 14.8 Duplication
Section 14.9 Slicing
Section 14.10 Typed Data
Section 14.11 Read-Only Buffers
Section 14.12 CharBuffers
Section 14.13 Memory-Mapped I/O
Chapter 15 Channels
Section 15.1 The Channel Interfaces
Section 15.2 File Channels
Section 15.3 Converting Between
Streams and Channels
Section 15.4 Socket Channels
Section 15.5 Server Socket Channels
Section 15.6 Datagram Channels
Chapter 16 Nonblocking I/O
Section 16.1 Nonblocking I/O
Trang 39Section 16.2 Selectable Channels
Section 16.3 Selectors
Section 16.4 Selection Keys
Section 16.5 Pipe Channels
Part PART V: The File System
Chapter 17 Working with Files
Section 17.1 Understanding Files
Section 17.2 Directories and Paths
Section 17.3 The File Class
Section 17.4 Filename Filters
Section 17.5 File Filters
Section 17.6 File Descriptors
Section 17.7 Random-Access Files
Section 17.8 General Techniques for
Cross-Platform File Access Code
Chapter 18 File Dialogs and Choosers
Section 18.1 File Dialogs
Section 18.2 JFileChooser
Section 18.3 File Viewer, Part 6
Part PART VI: Text
Trang 40Chapter 19 Character Sets and Unicode
Section 19.1 The Unicode Character Set
Section 19.2 UTF-16
Section 19.3 UTF-8
Section 19.4 Other Encodings
Section 19.5 Converting Between Byte
Arrays and Strings
Chapter 20 Readers and Writers
Section 20.1 The java.io.Writer Class
Section 20.2 The OutputStreamWriter
Class
Section 20.3 The java.io.Reader Class
Section 20.4 The InputStreamReader
Class
Section 20.5 Encoding Heuristics
Section 20.6 Character Array Readers
and Writers
Section 20.7 String Readers and Writers
Section 20.8 Reading and Writing Files
Section 20.9 Buffered Readers and
Writers
Trang 41Section 20.10 Print Writers
Section 20.11 Piped Readers and Writers
Section 20.12 Filtered Readers and
Writers
Section 20.13 File Viewer Finis
Chapter 21 Formatted I/O with java.text
Section 21.1 The Old Way
Section 21.2 Choosing a Locale
Section 21.3 Number Formats
Section 21.4 Specifying Width with
FieldPosition
Section 21.5 Parsing Input
Section 21.6 Decimal Formats
Part PART VII: Devices
Chapter 22 The Java Communications
Trang 42Section 22.4 Serial Ports
Section 22.5 Parallel Ports
Chapter 23 USB
Section 23.1 USB Architecture
Section 23.2 Finding Devices
Section 23.3 Controlling Devices
Section 23.4 Describing Devices
Section 23.5 Pipes
Section 23.6 IRPs
Section 23.7 Temperature Sensor
Example
Section 23.8 Hot Plugging
Chapter 24 The J2ME Generic
Trang 43Section 24.6 Sockets
Section 24.7 Server Sockets
Section 24.8 Datagrams
Chapter 25 Bluetooth
Section 25.1 The Bluetooth Protocol
Section 25.2 The Java Bluetooth API
Section 25.3 The Local Device
Section 25.4 Discovering Devices
Section 25.5 Remote Devices
Section 25.6 Service Records
Section 25.7 Talking to Devices
Part PART VIII: Appendix
Trang 44Java™ I/O, 2nd Edition
by Elliotte Rusty Harold
Copyright © 2006 O'Reilly Media, Inc.All rights reserved
Printed in the United States of America
Published by O'Reilly Media, Inc., 1005Gravenstein Highway North, Sebastopol,
(safari.oreilly.com) For more
information, contact our
Trang 45corporate/institutional sales department:(800) 998-9938 or
corporate@oreilly.com
Editor: Deb Cameron
Developmental
Editor: Mike Loukides
Production Editor: Philip Dangler
Copyeditor: Rachel Wheeler
Proofreader: Lydia Onofrei
Indexer: Johnna VanHoose Dinse
Trang 46Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrators: Robert Romano and Jessamyn
Read
Printing History:
March 1999: First Edition.
May 2006: Second Edition.
Trang 47Nutshell Handbook, the Nutshell
Handbook logo, and the O'Reilly logo areregistered trademarks of O'Reilly Media,
Inc Java I/O, Second Edition, the image
of a rabbit, and related trade dress aretrademarks of O'Reilly Media, Inc
Java and all Java-based trademarks andlogos are trademarks or registered
trademarks of Sun Microsystems, Inc., inthe United States and other countries.O'Reilly Media, Inc is independent of SunMicrosystems, Inc
Many of the designations used by
manufacturers and sellers to distinguishtheir products are claimed as trademarks.Where those designations appear in this
Trang 48book, and O'Reilly Media, Inc was aware
of a trademark claim, the designationshave been printed in caps or initial caps
While every precaution has been taken inthe preparation of this book, the publisherand author assume no responsibility forerrors or omissions, or for damages
resulting from the use of the informationcontained herein
ISBN: 0-596-52750-0
[M]
Trang 49In many ways, this book is a prequel to my
previous book, Java Network
Programming (O'Reilly) When writing
that book, I more or less assumed thatreaders were familiar with basic input andoutput in Java™that they knew how to useinput streams and output streams, convertbytes to characters, connect filter streams
to each other, and so forth
However, after that book was published, Ibegan to notice that a lot of the questions Igot from readers weren't as much aboutnetwork programming itself as they wereabout input and output (I/O in programmervernacular) When Java 1.1 was released
Trang 50with a vastly expanded java.io packageand many new I/O classes spread outacross the rest of the class library, it
became obvious that a book that
specifically addressed I/O was required.This is that book More specifically, it isthat book updated and expanded to coverthe even more impressive I/O capabilitiesintroduced in Java 1.4, 5, and 6 The I/Oclass libraries in Java are more powerfuland interesting than ever, and this bookshows you how to take full advantage ofthem Techniques you'll learn here
include:
Reading and writing files
Communicating over network sockets