Java Cookbook, 2nd Edition |
By Ian F. Darwin |
| |
Publisher | : O'Reilly |
Pub Date | : June 2004 |
ISBN | : 0-596-00701-9 |
Pages | : 862 |
| | |
| | Copyright |
| | Preface |
| | | Preface to the Second Edition |
| | | Preface to the First Edition |
| | | Who This Book Is For |
| | | What's in This Book? |
| | | Platform Notes |
| | | Other Books |
| | | Conventions Used in This Book |
| | | Comments and Questions |
| | | Getting the Source Code |
| | | Acknowledgments |
|
| | Chapter 1. Getting Started: Compiling, Running, and Debugging |
| | | Introduction |
| | | Section 1.1. Compiling and Running Java: JDK |
| | | Section 1.2. Editing and Compiling with a Color-Highlighting Editor |
| | | Section 1.3. Compiling, Running, and Testing with an IDE |
| | | Section 1.4. Using CLASSPATH Effectively |
| | | Section 1.5. Using the com.darwinsys API Classes from This Book |
| | | Section 1.6. Compiling the Source Code Examples from This Book |
| | | Section 1.7. Automating Compilation with Ant |
| | | Section 1.8. Running Applets |
| | | Section 1.9. Dealing with Deprecation Warnings |
| | | Section 1.10. Conditional Debugging Without #ifdef |
| | | Section 1.11. Debugging Printouts |
| | | Section 1.12. Maintaining Program Correctness with Assertions |
| | | Section 1.13. Debugging with JDB |
| | | Section 1.14. Unit Testing: Avoid the Need for Debuggers |
| | | Section 1.15. Getting Readable Tracebacks |
| | | Section 1.16. Finding More Java Source Code |
| | | Section 1.17. Program: Debug |
|
| | Chapter 2. Interacting with the Environment |
| | | Introduction |
| | | Section 2.1. Getting Environment Variables |
| | | Section 2.2. System Properties |
| | | Section 2.3. Writing JDK Release-Dependent Code |
| | | Section 2.4. Writing Operating System-Dependent Code |
| | | Section 2.5. Using Extensions or Other Packaged APIs |
| | | Section 2.6. Parsing Command-Line Arguments |
|
| | Chapter 3. Strings and Things |
| | | Introduction |
| | | Section 3.1. Taking Strings Apart with Substrings |
| | | Section 3.2. Taking Strings Apart with StringTokenizer |
| | | Section 3.3. Putting Strings Together with +, StringBuilder (JDK 1.5), and StringBuffer |
| | | Section 3.4. Processing a String One Character at a Time |
| | | Section 3.5. Aligning Strings |
| | | Section 3.6. Converting Between Unicode Characters and Strings |
| | | Section 3.7. Reversing a String by Word or by Character |
| | | Section 3.8. Expanding and Compressing Tabs |
| | | Section 3.9. Controlling Case |
| | | Section 3.10. Indenting Text Documents |
| | | Section 3.11. Entering Nonprintable Characters |
| | | Section 3.12. Trimming Blanks from the End of a String |
| | | Section 3.13. Parsing Comma-Separated Data |
| | | Section 3.14. Program: A Simple Text Formatter |
| | | Section 3.15. Program: Soundex Name Comparisons |
|
| | Chapter 4. Pattern Matching with Regular Expressions |
| | | Introduction |
| | | Section 4.1. Regular Expression Syntax |
| | | Section 4.2. Using regexes in Java: Test for a Pattern |
| | | Section 4.3. Finding the Matching Text |
| | | Section 4.4. Replacing the Matched Text |
| | | Section 4.5. Printing All Occurrences of a Pattern |
| | | Section 4.6. Printing Lines Containing a Pattern |
| | | Section 4.7. Controlling Case in Regular Expressions |
| | | Section 4.8. Matching "Accented" or Composite Characters |
| | | Section 4.9. Matching Newlines in Text |
| | | Section 4.10. Program: Apache Logfile Parsing |
| | | Section 4.11. Program: Data Mining |
| | | Section 4.12. Program: Full Grep |
|
| | Chapter 5. Numbers |
| | | Introduction |
| | | Section 5.1. Checking Whether a String Is a Valid Number |
| | | Section 5.2. Storing a Larger Number in a Smaller Number |
| | | Section 5.3. Converting Numbers to Objects and Vice Versa |
| | | Section 5.4. Taking a Fraction of an Integer Without Using Floating Point |
| | | Section 5.5. Ensuring the Accuracy of Floating-Point Numbers |
| | | Section 5.6. Comparing Floating-Point Numbers |
| | | Section 5.7. Rounding Floating-Point Numbers |
| | | Section 5.8. Formatting Numbers |
| | | Section 5.9. Converting Between Binary, Octal, Decimal, and Hexadecimal |
| | | Section 5.10. Operating on a Series of Integers |
| | | Section 5.11. Working with Roman Numerals |
| | | Section 5.12. Formatting with Correct Plurals |
| | | Section 5.13. Generating Random Numbers |
| | | Section 5.14. Generating Better Random Numbers |
| | | Section 5.15. Calculating Trigonometric Functions |
| | | Section 5.16. Taking Logarithms |
| | | Section 5.17. Multiplying Matrices |
| | | Section 5.18. Using Complex Numbers |
| | | Section 5.19. Handling Very Large Numbers |
| | | Section 5.20. Program: TempConverter |
| | | Section 5.21. Program: Number Palindromes |
|
| | Chapter 6. Dates and Times |
| | | Introduction |
| | | Section 6.1. Finding Today's Date |
| | | Section 6.2. Printing Date/Time in a Given Format |
| | | Section 6.3. Representing Dates in Other Epochs |
| | | Section 6.4. Converting YMDHMS to a Calendar or Epoch Seconds |
| | | Section 6.5. Parsing Strings into Dates |
| | | Section 6.6. Converting Epoch Seconds to DMYHMS |
| | | Section 6.7. Adding to or Subtracting from a Dateor Calendar |
| | | Section 6.8. Difference Between Two Dates |
| | | Section 6.9. Comparing Dates |
| | | Section 6.10. Day of Week/Month/Year or Week Number |
| | | Section 6.11. Creating a Calendar Page |
| | | Section 6.12. Measuring Elapsed Time |
| | | Section 6.13. Sleeping for a While |
| | | Section 6.14. Program: Reminder Service |
|
| | Chapter 7. Structuring Data with Java |
| | | Introduction |
| | | Section 7.1. Using Arrays for Data Structuring |
| | | Section 7.2. Resizing an Array |
| | | Section 7.3. Like an Array, but More Dynamic |
| | | Section 7.4. Using Iterators for Data-Independent Access |
| | | Section 7.5. Structuring Data in a Linked List |
| | | Section 7.6. Mapping with Hashtable and HashMap |
| | | Section 7.7. Storing Strings in Properties and Preferences |
| | | Section 7.8. Sorting a Collection |
| | | Section 7.9. Avoiding the Urge to Sort |
| | | Section 7.10. Eschewing Duplication |
| | | Section 7.11. Finding an Object in a Collection |
| | | Section 7.12. Converting a Collection to an Array |
| | | Section 7.13. Rolling Your Own Iterator |
| | | Section 7.14. Stack |
| | | Section 7.15. Multidimensional Structures |
| | | Section 7.16. Finally, Collections |
| | | Section 7.17. Program: Timing Comparisons |
|
| | Chapter 8. Data Structuring with Generics, foreach, and Enumerations (JDK 1.5) |
| | | Introduction |
| | | Section 8.1. Using Generic Collections |
| | | Section 8.2. Using "foreach" Loops |
| | | Section 8.3. Avoid Casting by Using Generics |
| | | Section 8.4. Let Java Convert with AutoBoxing and AutoUnboxing |
| | | Section 8.5. Using Typesafe Enumerations |
| | | Section 8.6. Program: MediaInvoicer |
|
| | Chapter 9. Object-Oriented Techniques |
| | | Introduction |
| | | Section 9.1. Printing Objects: Formatting with toString( ) |
| | | Section 9.2. Overriding the Equals Method |
| | | Section 9.3. Overriding the hashCode Method |
| | | Section 9.4. The Clone Method |
| | | Section 9.5. The Finalize Method |
| | | Section 9.6. Using Inner Classes |
| | | Section 9.7. Providing Callbacks via Interfaces |
| | | Section 9.8. Polymorphism/Abstract Methods |
| | | Section 9.9. Passing Values |
| | | Section 9.10. Enforcing the Singleton Pattern |
| | | Section 9.11. Roll Your Own Exceptions |
| | | Section 9.12. Program: Plotter |
|
| | Chapter 10. Input and Output |
| | | Introduction |
| | | Section 10.1. Reading Standard Input |
| | | Section 10.2. Writing Standard Output |
| | | Section 10.3. Printing with the 1.5 Formatter |
| | | Section 10.4. Scanning a File with StreamTokenizer |
| | | Section 10.5. Scanning Input with the 1.5 Scanner Class |
| | | Section 10.6. Opening a File by Name |
| | | Section 10.7. Copying a File |
| | | Section 10.8. Reading a File into a String |
| | | Section 10.9. Reassigning the Standard Streams |
| | | Section 10.10. Duplicating a Stream as It Is Written |
| | | Section 10.11. Reading/Writing a Different Character Set |
| | | Section 10.12. Those Pesky End-of-Line Characters |
| | | Section 10.13. Beware Platform-Dependent File Code |
| | | Section 10.14. Reading "Continued" Lines |
| | | Section 10.15. Binary Data |
| | | Section 10.16. Seeking |
| | | Section 10.17. Writing Data Streams from C |
| | | Section 10.18. Saving and Restoring Java Objects |
| | | Section 10.19. Preventing ClassCastExceptions with SerialVersionUID |
| | | Section 10.20. Reading and Writing JAR or Zip Archives |
| | | Section 10.21. Reading and Writing Compressed Files |
| | | Section 10.22. Program: Text to PostScript |
|
| | Chapter 11. Directory and Filesystem Operations |
| | | Introduction |
| | | Section 11.1. Getting File Information |
| | | Section 11.2. Creating a File |
| | | Section 11.3. Renaming a File |
| | | Section 11.4. Deleting a File |
| | | Section 11.5. Creating a Transient File |
| | | Section 11.6. Changing File Attributes |
| | | Section 11.7. Listing a Directory |
| | | Section 11.8. Getting the Directory Roots |
| | | Section 11.9. Creating New Directories |
| | | Section 11.10. Program: Find |
|
| | Chapter 12. Programming External Devices: Serial and Parallel Ports |
| | | Introduction |
| | | Section 12.1. Choosing a Port |
| | | Section 12.2. Opening a Serial Port |
| | | Section 12.3. Opening a Parallel Port |
| | | Section 12.4. Resolving Port Conflicts |
| | | Section 12.5. Reading and Writing: Lock-Step |
| | | Section 12.6. Reading and Writing: Event-Driven |
| | | Section 12.7. Reading and Writing: Threads |
| | | Section 12.8. Program: Penman Plotter |
|
| | Chapter 13. Graphics and Sound |
| | | Introduction |
| | | Section 13.1. Painting with a Graphics Object |
| | | Section 13.2. Testing Graphical Components |
| | | Section 13.3. Drawing Text |
| | | Section 13.4. Drawing Centered Text in a Component |
| | | Section 13.5. Drawing a Drop Shadow |
| | | Section 13.6. Drawing Text with 2D |
| | | Section 13.7. Drawing Text with an Application Font |
| | | Section 13.8. Drawing an Image |
| | | Section 13.9. Playing a Sound File |
| | | Section 13.10. Playing a Video Clip |
| | | Section 13.11. Printing in Java |
| | | Section 13.12. Program: PlotterAWT |
| | | Section 13.13. Program: Grapher |
|
| | Chapter 14. Graphical User Interfaces |
| | | Introduction |
| | | Section 14.1. Displaying GUI Components |
| | | Section 14.2. Designing a Window Layout |
| | | Section 14.3. A Tabbed View of Life |
| | | Section 14.4. Action Handling: Making Buttons Work |
| | | Section 14.5. Action Handling Using Anonymous Inner Classes |
| | | Section 14.6. Terminating a Program with"Window Close" |
| | | Section 14.7. Dialogs: When Later Just Won't Do |
| | | Section 14.8. Catching and Formatting GUI Exceptions |
| | | Section 14.9. Getting Program Output into a Window |
| | | Section 14.10. Choosing a Value with JSpinner |
| | | Section 14.11. Choosing a File with JFileChooser |
| | | Section 14.12. Choosing a Color |
| | | Section 14.13. Formatting JComponents with HTML |
| | | Section 14.14. Centering a Main Window |
| | | Section 14.15. Changing a Swing Program's Look and Feel |
| | | Section 14.16. Enhancing Your GUI for Mac OS X |
| | | Section 14.17. Program: Custom Font Chooser |
| | | Section 14.18. Program: Custom Layout Manager |
|
| | Chapter 15. Internationalization and Localization |
| | | Introduction |
| | | Section 15.1. Creating a Button with I18N Resources |
| | | Section 15.2. Listing Available Locales |
| | | Section 15.3. Creating a Menu with I18N Resources |
| | | Section 15.4. Writing Internationalization Convenience Routines |
| | | Section 15.5. Creating a Dialog with I18N Resources |
| | | Section 15.6. Creating a Resource Bundle |
| | | Section 15.7. Extracting Strings from Your Code |
| | | Section 15.8. Using a Particular Locale |
| | | Section 15.9. Setting the Default Locale |
| | | Section 15.10. Formatting Messages |
| | | Section 15.11. Program: MenuIntl |
| | | Section 15.12. Program: BusCard |
|
| | Chapter 16. Network Clients |
| | | Introduction |
| | | Section 16.1. Contacting a Server |
| | | Section 16.2. Finding and Reporting Network Addresses |
| | | Section 16.3. Handling Network Errors |
| | | Section 16.4. Reading and Writing Textual Data |
| | | Section 16.5. Reading and Writing Binary Data |
| | | Section 16.6. Reading and Writing Serialized Data |
| | | Section 16.7. UDP Datagrams |
| | | Section 16.8. Program: TFTP UDP Client |
| | | Section 16.9. Program: Telnet Client |
| | | Section 16.10. Program: Chat Client |
|
| | Chapter 17. Server-Side Java: Sockets |
| | | Introduction |
| | | Section 17.1. Opening a Server for Business |
| | | Section 17.2. Returning a Response (String or Binary) |
| | | Section 17.3. Returning Object Information |
| | | Section 17.4. Handling Multiple Clients |
| | | Section 17.5. Serving the HTTP Protocol |
| | | Section 17.6. Securing a Web Server with SSL and JSSE |
| | | Section 17.7. Network Logging |
| | | Section 17.8. Network Logging with log4j |
| | | Section 17.9. Network Logging with JDK 1.4 |
| | | Section 17.10. Finding Network Interfaces |
| | | Section 17.11. Program: A Java Chat Server |
|
| | Chapter 18. Network Clients II: Applets and Web Clients |
| | | Introduction |
| | | Section 18.1. Embedding Java in a Web Page |
| | | Section 18.2. Applet Techniques |
| | | Section 18.3. Contacting a Server on the Applet Host |
| | | Section 18.4. Making an Applet Show a Document |
| | | Section 18.5. Making an Applet Run JavaScript |
| | | Section 18.6. Making an Applet Run a CGI Script |
| | | Section 18.7. Reading the Contents of a URL |
| | | Section 18.8. URI, URL, or URN? |
| | | Section 18.9. Extracting HTML from a URL |
| | | Section 18.10. Extracting URLs from a File |
| | | Section 18.11. Converting a Filename to a URL |
| | | Section 18.12. Program: MkIndex |
| | | Section 18.13. Program: LinkChecker |
|
| | Chapter 19. Java and Electronic Mail |
| | | Introduction |
| | | Section 19.1. Sending Email: Browser Version |
| | | Section 19.2. Sending Email: For Real |
| | | Section 19.3. Mail-Enabling a Server Program |
| | | Section 19.4. Sending MIME Mail |
| | | Section 19.5. Providing Mail Settings |
| | | Section 19.6. Sending Mail Without Using JavaMail |
| | | Section 19.7. Reading Email |
| | | Section 19.8. Program: MailReaderBean |
| | | Section 19.9. Program: MailClient |
|
| | Chapter 20. Database Access |
| | | Introduction |
| | | Section 20.1. Easy Database Access with JDO |
| | | Section 20.2. Text-File Databases |
| | | Section 20.3. DBM Databases |
| | | Section 20.4. JDBC Setup and Connection |
| | | Section 20.5. Connecting to a JDBC Database |
| | | Section 20.6. Sending a JDBC Query and Getting Results |
| | | Section 20.7. Using JDBC Prepared Statements |
| | | Section 20.8. Using Stored Procedures with JDBC |
| | | Section 20.9. Changing Data Using a ResultSet |
| | | Section 20.10. Storing Results in a RowSet |
| | | Section 20.11. Changing Data Using SQL |
| | | Section 20.12. Finding JDBC Metadata |
| | | Section 20.13. Program: SQLRunner |
|
| | Chapter 21. XML |
| | | Introduction |
| | | Section 21.1. Generating XML from Objects |
| | | Section 21.2. Transforming XML with XSLT |
| | | Section 21.3. Parsing XML with SAX |
| | | Section 21.4. Parsing XML with DOM |
| | | Section 21.5. Verifying Structure with a DTD |
| | | Section 21.6. Generating Your Own XML with DOM |
| | | Section 21.7. Program: xml2mif |
|
| | Chapter 22. Distributed Java: RMI |
| | | Introduction |
| | | Section 22.1. Defining the RMI Contract |
| | | Section 22.2. Creating an RMI Client |
| | | Section 22.3. Creating an RMI Server |
| | | Section 22.4. Deploying RMI Across a Network |
| | | Section 22.5. Program: RMI Callbacks |
| | | Section 22.6. Program: NetWatch |
|
| | Chapter 23. Packages and Packaging |
| | | Introduction |
| | | Section 23.1. Creating a Package |
| | | Section 23.2. Documenting Classes with Javadoc |
| | | Section 23.3. Beyond JavaDoc: Annotations/Metadata (JDK 1.5) and XDoclet |
| | | Section 23.4. Archiving with jar |
| | | Section 23.5. Running an Applet from a JAR |
| | | Section 23.6. Running an Applet with a Modern JDK |
| | | Section 23.7. Running a Main Program from a JAR |
| | | Section 23.8. Preparing a Class as a JavaBean |
| | | Section 23.9. Pickling Your Bean into a JAR |
| | | Section 23.10. Packaging a Servlet into a WAR File |
| | | Section 23.11. "Write Once, Install Anywhere" |
| | | Section 23.12. "Write Once, Install on Mac OS X" |
| | | Section 23.13. Java Web Start |
| | | Section 23.14. Signing Your JAR File |
|
| | Chapter 24. Threaded Java |
| | | Introduction |
| | | Section 24.1. Running Code in a Different Thread |
| | | Section 24.2. Displaying a Moving Image with Animation |
| | | Section 24.3. Stopping a Thread |
| | | Section 24.4. Rendezvous and Timeouts |
| | | Section 24.5. Synchronizing Threads with the synchronized Keyword |
| | | Section 24.6. Simplifying Synchronization with 1.5 Locks |
| | | Section 24.7. Synchronizing Threads with wait( ) and notifyAll( ) |
| | | Section 24.8. Simplifying Producer-Consumer with the 1.5 Queue Interface |
| | | Section 24.9. Background Saving in an Editor |
| | | Section 24.10. Program: Threaded Network Server |
| | | Section 24.11. Simplifying Servers Using the Concurrency Utilities (JDK 1.5) |
|
| | Chapter 25. Introspection, or "A Class Named Class" |
| | | Introduction |
| | | Section 25.1. Getting a Class Descriptor |
| | | Section 25.2. Finding and Using Methods and Fields |
| | | Section 25.3. Loading and Instantiating a Class Dynamically |
| | | Section 25.4. Constructing a Class from Scratch |
| | | Section 25.5. Performance Timing |
| | | Section 25.6. Printing Class Information |
| | | Section 25.7. Program: CrossRef |
| | | Section 25.8. Program: AppletViewer |
|
| | Chapter 26. Using Java with Other Languages |
| | | Introduction |
| | | Section 26.1. Running a Program |
| | | Section 26.2. Running a Program and Capturing Its Output |
| | | Section 26.3. Mixing Java and Scripts with BSF |
| | | Section 26.4. Marrying Java and Perl |
| | | Section 26.5. Blending in Native Code (C/C++) |
| | | Section 26.6. Calling Java from Native Code |
| | | Section 26.7. Program: DBM |
|
| | Afterword |
| | Colophon |
| | Index |