Java I/O, 2nd Edition

Book description

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 application. The universe of streams is divided into four largecategories: 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 CipherInputStream for reading encrypted data? And a ZipOutputStream for automaticallycompressing data? Do you know how to use buffered streams effectively 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 definitions for the character sets of most written languages. Consequently, Java is the first programming language that lets you do I/O in virtually any language. Java also provides a sophisticated model 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

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1.  
    2. What’s New in This Edition
    3. Organization of the Book
      1. Part I: Basic I/O
      2. Part II: Data Sources
      3. Part III: Filter Streams
      4. Part IV: New I/O
      5. Part V: The File System
      6. Part VI: Text
      7. Part VII: Devices
    4. Who You Are
    5. About the Examples
    6. Conventions Used in This Book
    7. Request for Comments
    8. Safari Enabled
    9. Acknowledgments
  2. I. Basic I/O
    1. 1. Introducing I/O
      1. What Is a Stream?
        1. Where Do Streams Come From?
        2. The Stream Classes
      2. Numeric Data
        1. Integer Data
        2. Conversions and Casts
      3. Character Data
        1. ASCII
        2. Latin-1
        3. Unicode
        4. Other Encodings
        5. The char Data Type
      4. Readers and Writers
      5. Buffers and Channels
      6. The Ubiquitous IOException
      7. The Console: System.out, System.in, and System.err
        1. System.out
        2. System.err
        3. System.in
        4. Redirecting System.out, System.in, and System.err
        5. The Console Class // Java 6
      8. Security Checks on I/O
    2. 2. Output Streams
      1. Writing Bytes to Output Streams
      2. Writing Arrays of Bytes
      3. Closing Output Streams
        1. The Closeable Interface
      4. Flushing Output Streams
        1. The Flushable Interface
      5. Subclassing OutputStream
      6. A Graphical User Interface for Output Streams
    3. 3. Input Streams
      1. The read( ) Method
      2. Reading Chunks of Data from a Stream
      3. Counting the Available Bytes
      4. Skipping Bytes
      5. Closing Input Streams
      6. Marking and Resetting
      7. Subclassing InputStream
      8. An Efficient Stream Copier
  3. II. Data Sources
    1. 4. File Streams
      1. Reading Files
      2. Writing Files
      3. File Viewer, Part 1
    2. 5. Network Streams
      1. URLs
      2. URL Connections
        1. Reading Data from URL Connections
        2. Writing Data on URL Connections
      3. Sockets
      4. Server Sockets
      5. URLViewer
  4. III. Filter Streams
    1. 6. Filter Streams
      1. The Filter Stream Classes
      2. The Filter Stream Subclasses
      3. Buffered Streams
        1. BufferedInputStream Details
        2. BufferedOutputStream Details
      4. PushbackInputStream
      5. ProgressMonitorInputStream
      6. Multitarget Output Streams
      7. File Viewer, Part 2
    2. 7. Print Streams
      1. Print Versus Write
      2. Line Breaks
      3. Error Handling
      4. printf( )
      5. Formatter
        1. Constructors
        2. Character Sets
        3. Locales
        4. Error Handling
        5. Format Specifiers
          1. Integer conversions
          2. Floating-point conversions
          3. Date and time conversions
          4. Character conversions
          5. Boolean conversions
          6. General conversions
        6. Format Modifiers
          1. Argument index
          2. Flags
          3. Width
          4. Precision
      6. Formattable
    3. 8. Data Streams
      1. The Data Stream Classes
        1. The DataInput and DataOutput Interfaces
        2. Constructors
      2. Integers
        1. Integer Formats
        2. The Char Format
        3. Writing Integers
        4. Reading Integers
      3. Floating-Point Numbers
        1. Writing Floating-Point Numbers
        2. Reading Floating-Point Numbers
      4. Booleans
      5. Byte Arrays
        1. Determining the Number of Bytes Written
        2. Skipping Bytes
      6. Strings and chars
        1. Writing Text
        2. Reading Text
        3. The Deprecated readLine( ) Method
      7. Little-Endian Numbers
      8. Thread Safety
      9. File Viewer, Part 3
    4. 9. Streams in Memory
      1. Sequence Input Streams
      2. Byte Array Streams
        1. Byte Array Input Streams
        2. Byte Array Output Streams
      3. Communicating Between Threads Using Piped Streams
    5. 10. Compressing Streams
      1. Inflaters and Deflaters
        1. Deflating Data
          1. Constructing deflaters
          2. Choose a strategy
          3. Set the compression level
          4. Set the dictionary
          5. Set the input
          6. Deflate the data repeatedly until needsInput( ) returns true
          7. Finish the deflation
          8. Reset the deflater and start over
          9. An example
          10. Checking the state of a deflater
        2. Inflating Data
          1. Constructing inflaters
          2. Set the input
          3. Check whether a preset dictionary was used
          4. Set the dictionary
          5. Inflate the data
          6. Reset the inflater
          7. An example
          8. Checking the state of an inflater
      2. Compressing and Decompressing Streams
        1. The DeflaterOutputStream Class
        2. The InflaterInputStream Class
        3. The GZIPOutputStream Class
        4. The GZIPInputStream Class
        5. Expanding Output Streams and Compressing Input Streams
      3. Zip Files
        1. Zip Entries
        2. The ZipOutputStream Class
          1. Constructing and initializing the ZipOutputStream
          2. Set the comment for the zip file
          3. Set the default compression level and method
          4. Construct a ZipEntry object and put it in the archive
          5. Write the entry’s data onto the output stream
          6. Close the zip entry
          7. Finish the zip output stream
          8. Close the zip output stream
          9. An example
        3. The ZipInputStream Class
          1. Construct a ZipInputStream
          2. Open the next zip entry
          3. Reading from a ZipInputStream
          4. Close the zip entry
          5. Close the ZipInputStream
          6. An example
      4. Checksums
        1. Checked Streams
      5. File Viewer, Part 4
    6. 11. JAR Archives
      1. Meta-Information: Manifest Files and Signatures
      2. The jar Tool
      3. The java.util.jar Package
      4. JarFile
      5. JarEntry
      6. Attributes
      7. Manifest
      8. JarInputStream
      9. JarOutputStream
      10. JarURLConnection
      11. Pack200
      12. Reading Resources from JAR Files
    7. 12. Cryptographic Streams
      1. Hash Functions
        1. Requirements for Hash Functions
      2. The MessageDigest Class
        1. Calculating Message Digests
        2. Creating Message Digests
        3. Feeding Data to the Digest
        4. Finishing the Digest
        5. Reusing Digests
        6. Comparing Digests
        7. Accessor Methods
      3. Digest Streams
        1. DigestInputStream
        2. DigestOutputStream
      4. Encryption Basics
        1. Keys
        2. Secret Key Versus Public Key Algorithms
        3. Block Versus Stream Ciphers
        4. Key Management
      5. The Cipher Class
        1. init( )
          1. Mode
          2. Key
          3. Algorithm parameters
          4. Source of randomness
        2. update( )
        3. doFinal( )
        4. Accessor Methods
      6. Cipher Streams
        1. CipherInputStream
        2. CipherOutputStream
      7. File Viewer, Part 5
    8. 13. Object Serialization
      1. Reading and Writing Objects
      2. Object Streams
      3. How Object Serialization Works
      4. Performance
      5. The Serializable Interface
        1. Classes That Implement Serializable but Aren’t
          1. Problem 1: References to nonserializable objects
          2. Problem 2: Missing a no-argument constructor in superclass
          3. Problem 3: Deliberate throwing of NotSerializableException
          4. Locating the offending object
          5. Making nonserializable fields transient
      6. Versioning
        1. Compatible and Incompatible Changes
        2. SUIDs
      7. Customizing the Serialization Format
        1. The readObject( ) and writeObject( ) Methods
        2. The defaultWriteObject() and defaultReadObject( ) Methods
        3. The writeReplace( ) Method
        4. The readResolve( ) Method
        5. serialPersistentFields
        6. Preventing Serialization
        7. Externalizable
      8. Resolving Classes
      9. Resolving Objects
      10. Validation
      11. Sealed Objects
      12. JavaDoc
        1. @serial
        2. @serialData
        3. @serialField
  5. IV. New I/O
    1. 14. Buffers
      1. Copying Files with Buffers
      2. Creating Buffers
      3. Buffer Layout
        1. Limit
      4. Bulk Put and Get
      5. Absolute Put and Get
      6. Mark and Reset
      7. Compaction
      8. Duplication
      9. Slicing
      10. Typed Data
        1. View Buffers
        2. Put Type Methods
        3. Byte Order
      11. Read-Only Buffers
      12. CharBuffers
      13. Memory-Mapped I/O
        1. Creating Mapped Byte Buffers
        2. MappedByteBuffer Methods
    2. 15. Channels
      1. The Channel Interfaces
        1. Channel
        2. ReadableByteChannel and WritableByteChannel
          1. ByteChannel
          2. Exceptions
        3. Gathering and Scattering Channels
      2. File Channels
        1. Transferring Data
        2. Random Access
        3. Threading and Locking
        4. FileLock
        5. Flushing
      3. Converting Between Streams and Channels
        1. Converting Channels to Streams
        2. Converting Streams to Channels
        3. Converting Channels to Readers and Writers
      4. Socket Channels
      5. Server Socket Channels
      6. Datagram Channels
        1. Connecting
        2. Reading
        3. Writing
    3. 16. Nonblocking I/O
      1. Nonblocking I/O
      2. Selectable Channels
      3. Selectors
      4. Selection Keys
        1. Getters
        2. Attachments
        3. Canceling
      5. Pipe Channels
  6. V. The File System
    1. 17. Working with Files
      1. Understanding Files
        1. Filenames
        2. File Attributes
        3. Filename Extensions and File Types
      2. Directories and Paths
        1. Paths and Separators
        2. Relative versus Absolute Paths
          1. Absolute paths
          2. Relative paths
      3. The File Class
        1. Constructing File Objects
        2. Listing the Roots
        3. Listing Information about a File
          1. Does the file exist? Is it a normal file? Is it a directory?
          2. Filename and path
          3. Absolute paths
          4. Canonical paths
          5. Parents
          6. File attributes
          7. An example
        4. Manipulating Files
          1. Creating files
          2. Moving and renaming files
          3. Deleting files
          4. Changing file attributes
        5. Temporary Files
        6. Checking for Free Space/Java 6
        7. Directories
          1. Creating directories
          2. Listing directories
          3. The listFiles( ) methods
        8. File URLs
      4. Filename Filters
      5. File Filters
      6. File Descriptors
      7. Random-Access Files
      8. General Techniques for Cross-Platform File Access Code
    2. 18. File Dialogs and Choosers
      1. File Dialogs
      2. JFileChooser
        1. Constructing File Choosers
        2. Displaying File Choosers
        3. Getting the User’s Selection
        4. Manipulating the JFileChooser
        5. Custom Dialogs
        6. Filters
        7. Selecting Directories
        8. Multiple Selections
        9. Hidden Files
        10. File Views
        11. FileSystem Views
        12. Handling Events
          1. Action events
          2. Property change events
        13. Accessory
      3. File Viewer, Part 6
  7. VI. Text
    1. 19. Character Sets and Unicode
      1. The Unicode Character Set
      2. UTF-16
      3. UTF-8
      4. Other Encodings
      5. Converting Between Byte Arrays and Strings
        1. The String Class
        2. The Charset Class
          1. Retrieving Charset objects
          2. Character set info
          3. Encoding and decoding
        3. CharsetEncoder and CharsetDecoder
          1. Encoding
          2. Decoding
          3. Error handling
          4. Measurement
          5. Encodability
    2. 20. Readers and Writers
      1. The java.io.Writer Class
      2. The OutputStreamWriter Class
      3. The java.io.Reader Class
      4. The InputStreamReader Class
      5. Encoding Heuristics
      6. Character Array Readers and Writers
        1. The CharArrayWriter Class
        2. The CharArrayReader Class
      7. String Readers and Writers
        1. String Writers
        2. String Readers
      8. Reading and Writing Files
        1. FileWriter
        2. FileReader
      9. Buffered Readers and Writers
        1. Buffering Writes
        2. Buffering Reads
        3. Line Numbering
      10. Print Writers
      11. Piped Readers and Writers
      12. Filtered Readers and Writers
        1. The FilterReader Class
        2. The FilterWriter Class
        3. PushbackReader
      13. File Viewer Finis
    3. 21. Formatted I/O with java.text
      1. The Old Way
      2. Choosing a Locale
      3. Number Formats
        1. Formatting Numbers
        2. Specifying Precision
        3. Grouping
        4. Currency Formats
        5. Percent Formats
      4. Specifying Width with FieldPosition
      5. Parsing Input
      6. Decimal Formats
        1. Decimal Format Patterns
        2. DecimalFormatSymbols
        3. Constructing Decimal Formats with Patterns and Symbols
  8. VII. Devices
    1. 22. The Java Communications API
      1. The Architecture of the Java Communications API
      2. Identifying Ports
        1. Finding the Ports
        2. Getting Information about a Port
        3. Opening Ports
      3. Communicating with a Device on a Port
        1. Communicating with a Port
        2. Port Properties
      4. Serial Ports
        1. Control Functions
        2. Flow Control
        3. Control Wires
          1. DTR
          2. RTS
          3. CTS
          4. DSR
          5. RI
          6. CD
        4. Serial Port Events
          1. SerialPortEventListener
          2. SerialPortEvent
      5. Parallel Ports
        1. Parallel Port Modes
        2. Controlling the Parallel Port
        3. Checking the State of the Port
        4. Parallel Port Events
          1. Parallel Port Event Listeners
          2. ParallelPortEvent
    2. 23. USB
      1. USB Architecture
      2. Finding Devices
      3. Controlling Devices
      4. Describing Devices
        1. UsbDevice
        2. UsbDeviceDescriptor
        3. USB Configurations
        4. UsbConfigurationDescriptor
        5. UsbInterface
          1. Settings
          2. Claiming
        6. UsbInterfaceDescriptor
        7. UsbEndpoints
      5. Pipes
      6. IRPs
      7. Temperature Sensor Example
      8. Hot Plugging
    3. 24. The J2ME Generic Connection Framework
      1. The Generic Connection Framework
        1. The Connector Class
      2. ContentConnection
      3. Files
        1.  
          1. File attributes
          2. Listing directories
        2. Filesystem Listeners
      4. HTTP
        1. Getter Methods
        2. Configuring the HTTP Request Header
        3. Reading the HTTP Response Header
      5. Serial I/O
      6. Sockets
        1. Getters
        2. Socket Options
      7. Server Sockets
      8. Datagrams
        1. Datagram URLs
    4. 25. Bluetooth
      1. The Bluetooth Protocol
      2. The Java Bluetooth API
        1. UUIDs
        2. The Bluetooth Control Center
        3. Initialization
      3. The Local Device
        1. Properties
        2. Device Class
        3. Discoverability
      4. Discovering Devices
      5. Remote Devices
      6. Service Records
        1. The DataElement Class
        2. Finding Service Records
        3. The ServiceRecord Interface
      7. Talking to Devices
        1. RFCOMM Clients
        2. L2CAP Devices
  9. VIII. Appendix
    1. A. Character Sets
  10. Index
  11. About the Author
  12. Colophon
  13. Copyright

Product information

  • Title: Java I/O, 2nd Edition
  • Author(s): Elliotte Rusty Harold
  • Release date: May 2006
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596527501