• • • • • • Table of Contents Index Reviews Reader Reviews Errata Academic Java Cookbook, 2nd Edition By Ian F Darwin Publisher : O'Reilly Pub Date : June 2004 ISBN : 0-596-00701-9 Pages : 862 Java Cookbook, 2nd Edition gets you to the heart of what you need to know when you need to know it The completely revised and updated recipes in Java Cookbook, 2nd Edition cover all of the major APIs from Java 1.4 as well as the new 1.5 version It includes many specialized APIs like those for working with Struts, Ant, and other Open Source tools and delivers expanded Mac OS coverage • • • • • • Table of Contents Index Reviews Reader Reviews Errata Academic 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 Platform Notes Conventions Used in This Book Getting the Source Code What's in This Book? Other Books Comments and Questions 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.4 Using CLASSPATH Effectively Section 1.6 Compiling the Source Code Examples from This Book Section 1.8 Running Applets Section 1.10 Conditional Debugging Without #ifdef Section 1.12 Maintaining Program Correctness with Assertions Section 1.14 Unit Testing: Avoid the Need for Debuggers Section 1.16 Finding More Java Source Code Section 1.3 Compiling, Running, and Testing with an IDE Section 1.5 Using the com.darwinsys API Classes from This Book Section 1.7 Automating Compilation with Ant Section 1.9 Dealing with Deprecation Warnings Section 1.11 Debugging Printouts Section 1.13 Debugging with JDB Section 1.15 Getting Readable Tracebacks 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.4 Writing Operating System-Dependent Code Section 2.6 Parsing Command-Line Arguments Section 2.3 Writing JDK Release-Dependent Code Section 2.5 Using Extensions or Other Packaged APIs 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.8 Expanding and Compressing Tabs Section 3.10 Indenting Text Documents Section 3.7 Reversing a String by Word or by Character Section 3.9 Controlling Case 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.15 Program: Soundex Name Comparisons Section 3.14 Program: A Simple Text Formatter 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.4 Replacing the Matched Text Section 4.6 Printing Lines Containing a Pattern Section 4.8 Matching "Accented" or Composite Characters Section 4.10 Program: Apache Logfile Parsing Section 4.12 Program: Full Grep Section 4.3 Finding the Matching Text Section 4.5 Printing All Occurrences of a Pattern Section 4.7 Controlling Case in Regular Expressions Section 4.9 Matching Newlines in Text Section 4.11 Program: Data Mining 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.4 Taking a Fraction of an Integer Without Using Floating Point Section 5.6 Comparing Floating-Point Numbers Section 5.8 Formatting Numbers Section 5.10 Operating on a Series of Integers Section 5.12 Formatting with Correct Plurals Section 5.14 Generating Better Random Numbers Section 5.16 Taking Logarithms Section 5.18 Using Complex Numbers Section 5.3 Converting Numbers to Objects and Vice Versa Section 5.5 Ensuring the Accuracy of Floating-Point Numbers Section 5.7 Rounding Floating-Point Numbers Section 5.9 Converting Between Binary, Octal, Decimal, and Hexadecimal Section 5.11 Working with Roman Numerals Section 5.13 Generating Random Numbers Section 5.15 Calculating Trigonometric Functions Section 5.17 Multiplying Matrices 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.4 Converting YMDHMS to a Calendar or Epoch Seconds Section 6.6 Converting Epoch Seconds to DMYHMS Section 6.8 Difference Between Two Dates Section 6.10 Day of Week/Month/Year or Week Number Section 6.12 Measuring Elapsed Time Section 6.14 Program: Reminder Service Section 6.3 Representing Dates in Other Epochs Section 6.5 Parsing Strings into Dates Section 6.7 Adding to or Subtracting from a Dateor Calendar Section 6.9 Comparing Dates Section 6.11 Creating a Calendar Page Section 6.13 Sleeping for a While Chapter 7 Structuring Data with Java Introduction Section 7.1 Using Arrays for Data Structuring Section 7.2 Resizing an Array Section 7.4 Using Iterators for Data-Independent Access Section 7.6 Mapping with Hashtable and HashMap Section 7.8 Sorting a Collection Section 7.10 Eschewing Duplication Section 7.12 Converting a Collection to an Array Section 7.14 Stack Section 7.16 Finally, Collections Section 7.3 Like an Array, but More Dynamic Section 7.5 Structuring Data in a Linked List Section 7.7 Storing Strings in Properties and Preferences Section 7.9 Avoiding the Urge to Sort Section 7.11 Finding an Object in a Collection Section 7.13 Rolling Your Own Iterator Section 7.15 Multidimensional Structures 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.3 Avoid Casting by Using Generics Section 8.5 Using Typesafe Enumerations Section 8.2 Using "foreach" Loops Section 8.4 Let Java Convert with AutoBoxing and AutoUnboxing 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.4 The Clone Method Section 9.6 Using Inner Classes Section 9.8 Polymorphism/Abstract Methods Section 9.10 Enforcing the Singleton Pattern Section 9.12 Program: Plotter Section 9.3 Overriding the hashCode Method Section 9.5 The Finalize Method Section 9.7 Providing Callbacks via Interfaces Section 9.9 Passing Values Section 9.11 Roll Your Own Exceptions Chapter 10 Input and Output Introduction Section 10.1 Reading Standard Input Section 10.2 Writing Standard Output Section 10.4 Scanning a File with StreamTokenizer Section 10.6 Opening a File by Name Section 10.8 Reading a File into a String Section 10.10 Duplicating a Stream as It Is Written Section 10.12 Those Pesky End-of-Line Characters Section 10.14 Reading "Continued" Lines Section 10.16 Seeking Section 10.3 Printing with the 1.5 Formatter Section 10.5 Scanning Input with the 1.5 Scanner Class Section 10.7 Copying a File Section 10.9 Reassigning the Standard Streams Section 10.11 Reading/Writing a Different Character Set Section 10.13 Beware Platform-Dependent File Code Section 10.15 Binary Data Section 10.17 Writing Data Streams from C Section 10.18 Saving and Restoring Java Objects Section 10.20 Reading and Writing JAR or Zip Archives Section 10.22 Program: Text to PostScript Section 10.19 Preventing ClassCastExceptions with SerialVersionUID Section 10.21 Reading and Writing Compressed Files Chapter 11 Directory and Filesystem Operations Introduction Section 11.1 Getting File Information Section 11.2 Creating a File Section 11.4 Deleting a File Section 11.6 Changing File Attributes Section 11.8 Getting the Directory Roots Section 11.10 Program: Find Section 11.3 Renaming a File Section 11.5 Creating a Transient File Section 11.7 Listing a Directory Section 11.9 Creating New Directories 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.4 Resolving Port Conflicts Section 12.6 Reading and Writing: Event-Driven Section 12.8 Program: Penman Plotter Section 12.3 Opening a Parallel Port Section 12.5 Reading and Writing: Lock-Step Section 12.7 Reading and Writing: Threads Chapter 13 Graphics and Sound Introduction Section 13.1 Painting with a Graphics Object Section 13.2 Testing Graphical Components Section 13.4 Drawing Centered Text in a Component Section 13.6 Drawing Text with 2D Section 13.8 Drawing an Image Section 13.3 Drawing Text Section 13.5 Drawing a Drop Shadow Section 13.7 Drawing Text with an Application Font Section 13.9 Playing a Sound File Section 13.10 Playing a Video Clip Section 13.12 Program: PlotterAWT Section 13.11 Printing in Java 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.4 Action Handling: Making Buttons Work Section 14.6 Terminating a Program with"Window Close" Section 14.8 Catching and Formatting GUI Exceptions Section 14.10 Choosing a Value with JSpinner Section 14.12 Choosing a Color Section 14.14 Centering a Main Window Section 14.16 Enhancing Your GUI for Mac OS X Section 14.18 Program: Custom Layout Manager Section 14.3 A Tabbed View of Life Section 14.5 Action Handling Using Anonymous Inner Classes Section 14.7 Dialogs: When Later Just Won't Do Section 14.9 Getting Program Output into a Window Section 14.11 Choosing a File with JFileChooser Section 14.13 Formatting JComponents with HTML Section 14.15 Changing a Swing Program's Look and Feel Section 14.17 Program: Custom Font Chooser Chapter 15 Internationalization and Localization Introduction Section 15.1 Creating a Button with I18N Resources Section 15.2 Listing Available Locales Section 15.4 Writing Internationalization Convenience Routines Section 15.6 Creating a Resource Bundle Section 15.8 Using a Particular Locale Section 15.10 Formatting Messages Section 15.12 Program: BusCard Section 15.3 Creating a Menu with I18N Resources Section 15.5 Creating a Dialog with I18N Resources Section 15.7 Extracting Strings from Your Code Section 15.9 Setting the Default Locale Section 15.11 Program: MenuIntl Chapter 16 Network Clients Introduction Section 16.2 Finding and Reporting Network Addresses Section 16.4 Reading and Writing Textual Data Section 16.6 Reading and Writing Serialized Data Section 16.8 Program: TFTP UDP Client Section 16.10 Program: Chat Client Section 16.1 Contacting a Server Section 16.3 Handling Network Errors Section 16.5 Reading and Writing Binary Data Section 16.7 UDP Datagrams Section 16.9 Program: Telnet 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.4 Handling Multiple Clients Section 17.6 Securing a Web Server with SSL and JSSE Section 17.8 Network Logging with log4j Section 17.10 Finding Network Interfaces Section 17.3 Returning Object Information Section 17.5 Serving the HTTP Protocol Section 17.7 Network Logging Section 17.9 Network Logging with JDK 1.4 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.4 Making an Applet Show a Document Section 18.6 Making an Applet Run a CGI Script Section 18.8 URI, URL, or URN? Section 18.10 Extracting URLs from a File Section 18.12 Program: MkIndex Section 18.3 Contacting a Server on the Applet Host Section 18.5 Making an Applet Run JavaScript Section 18.7 Reading the Contents of a URL Section 18.9 Extracting HTML from a URL Section 18.11 Converting a Filename to a URL Section 18.13 Program: LinkChecker ... Errata Academic 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. .. At the high end, the Java 2 Enterprise Edition (J2EE) is concerned with building large, scalable, distributed applications Servlets, JavaServer Pages, JavaServer Faces, CORBA, RMI, JavaMail, Enterprise JavaBeans© (EJBs), Transactions, and other APIs are part of... production work on the second edition Preface to the First Edition If you know a little Java, great If you know more Java, even better! This book is ideal for anyone who knows some Java and wants to learn more