Cover image for Java I/O

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 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 CipherInputStream for reading encrypted data? And a ZipOutputStream for automatically compressing data? Do you know how to use buffered streams effectively to make your I/O operations more efficient? Java I/O 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 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 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

Table of Contents

  1. Java I/O
    1. Dedication
    2. Preface
      1. Correcting Misconceptions
      2. Organization of the Book
        1. Part I: Basic I/O
        2. Part II: Data Sources
        3. Part III: Filter Streams
        4. Part IV: Advanced and Miscellaneous Topics
      3. Who You Are
      4. Versions
      5. Security Issues
      6. Conventions Used in This Book
      7. Request for Comments
      8. Acknowledgments
    3. 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. ISO Latin-1
          3. Unicode
          4. UTF-8
            1. Other encodings
          5. The char Data Type
        4. Readers and Writers
        5. The Ubiquitous IOException
        6. The Console: System.out, System.in, and System.err
          1. Consoles in Applets
          2. System.out
          3. System.err
          4. System.in
          5. Redirecting System.out, System.in, and System.err
        7. Security Checks on I/O
      2. 2. Output Streams
        1. The OutputStream Class
        2. Writing Bytes to Output Streams
        3. Writing Arrays of Bytes
        4. Flushing and Closing Output Streams
        5. Subclassing OutputStream
        6. A Graphical User Interface for Output Streams
      3. 3. Input Streams
        1. The InputStream Class
        2. The read( ) Method
        3. Reading Chunks of Data from a Stream
        4. Counting the Available Bytes
        5. Skipping Bytes
        6. Closing Input Streams
        7. Marking and Resetting
        8. Subclassing InputStream
        9. An Efficient Stream Copier
    4. 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
    5. 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. Print Streams
        6. Multitarget Output Streams
        7. File Viewer, Part 2
      2. 7. Data Streams
        1. The Data Stream Classes
          1. The DataInput and DataOutput Interfaces
          2. Constructors
        2. Reading and Writing Integers
          1. Integer Formats
          2. The Char Format
          3. Writing Integers
          4. Reading Integers
        3. Reading and Writing Floating-Point Numbers
          1. Writing Floating-Point Numbers
          2. Reading Floating-Point Numbers
        4. Reading and Writing Booleans
        5. Reading Byte Arrays
        6. Reading and Writing Text
          1. The UTF-8 Format
          2. Writing Text
          3. Reading Text
          4. The Deprecated readLine( ) Method
        7. Miscellaneous Methods
          1. Determining the Number of Bytes Written
          2. Skipping Bytes in an Input Stream
        8. Reading and Writing Little-Endian Numbers
        9. Thread Safety
        10. File Viewer, Part 3
      3. 8. 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 with Piped Streams
      4. 9. 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. Working with 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. JAR Files
          1. Meta-Infomation: 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
        6. File Viewer, Part 4
      5. 10. Cryptographic Streams
        1. Hash Function Basics
          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
          8. Cloning Digests
          9. toString( )
        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
    6. IV. Advanced and Miscellaneous Topics
      1. 11. 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. The ObjectInput and ObjectOutput Interfaces
        7. Versioning
          1. Compatible and Incompatible Changes
          2. Version IDs
        8. Customizing the Serialization Format
          1. The readObject( ) and writeObject( ) Methods
          2. The default WriteObject( ) and defaultReadObject( ) Methods
          3. Preventing Serialization
          4. Externalizable
        9. Resolving Classes
        10. Resolving Objects
        11. Validation
        12. Sealed Objects
      2. 12. 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. Copying files
            4. Deleting files
            5. Changing file attributes
          5. Temporary Files
          6. Utility Methods
          7. Working with 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
      3. 13. File Dialogs and Choosers
        1. File Dialogs
        2. JFileChooser
          1. Constructing File Choosers
          2. Displaying File Choosers
          3. Getting the User’s Selection
          4. Programmatically 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
          14. Swing Methods
        3. File Viewer, Part 6
      4. 14. Multilingual Character Sets and Unicode
        1. Unicode
        2. Displaying Unicode Text
        3. Unicode Escapes
        4. UTF-8
        5. The char Data Type
          1. The java.lang.Character Class
            1. Constructor
            2. Instance methods
            3. Character types
            4. Identifiers
            5. Numeric values
            6. Character subsets
            7. Unicode blocks
        6. Other Encodings
        7. Converting Between Byte Arrays and Strings
      5. 15. Readers and Writers
        1. The java.io.Writer Class
        2. The OutputStreamWriter Class
        3. The java.io.Reader Class
        4. The InputStreamReader Class
        5. Character Array Readers and Writers
          1. The CharArrayWriter Class
          2. The CharArrayReader Class
        6. String Readers and Writers
          1. String Writers
          2. String Readers
        7. Reading and Writing Files
          1. FileWriter
          2. FileReader
        8. Buffered Readers and Writers
          1. Buffering Writes for Better Performance
          2. Buffering Reads for Better Performance
          3. Line Numbering
        9. Print Writers
        10. Piped Readers and Writers
        11. Filtered Readers and Writers
          1. The FilterReader Class
          2. The FilterWriter Class
          3. PushbackReader
        12. File Viewer Finis
      6. 16. 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
          6. Utility Methods
        4. Specifying Width with FieldPosition
        5. Parsing Input
        6. Decimal Formats
          1. Decimal Format Patterns and Symbols
          2. DecimalFormatSymbols
            1. Utility methods
          3. Constructing Decimal Formats with Patterns and Symbols
        7. An Exponential Number Format
      7. 17. 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
          4. Waiting for a Port with Port Ownership Events
          5. Registering 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
          5. Serial Port Event Listeners
            1. Step 1
            2. Step 2
            3. Step 3
        5. Parallel Ports
          1. Parallel Port Modes
          2. Controlling the Parallel Port
          3. Checking the State of the Port
          4. Parallel Port Events
          5. Parallel Port Event Listeners
            1. Step 1
            2. Step 2
            3. Step 3
    7. V. Appendixes
      1. A. Additional Resources
        1. Digital Think
        2. Design Patterns
        3. The java.io Package
        4. Network Programming
        5. Data Compression
        6. Encryption and Related Technology
        7. Object Serialization
        8. International Character Sets and Unicode
        9. Java Communications API
        10. Updates and Breaking News
          1. Café au Lait
          2. java.oreilly.com
          3. JavaWorld
      2. B. Character Sets
    8. Index
    9. Colophon