Java Cookbook, Second Edition

Java Cookbook, Second Edition

by Ian F Darwin

Paperback(Second Edition)

$41.76 $49.99 Save 16% Current price is $41.76, Original price is $49.99. You Save 16%.

Product Details

ISBN-13: 9780596007010
Publisher: O'Reilly Media, Incorporated
Publication date: 06/28/2004
Edition description: Second Edition
Pages: 864
Product dimensions: 7.14(w) x 9.22(h) x 1.54(d)

About the Author

Ian F. Darwin has worked in the computer industry for three decades: with Unix since 1980, Java since 1995, and OpenBSD since 1998. He wrote the freeware file(1) command used on Linux and BSD and is the author of Checking C Programs with Lint, Java Cookbook, and over seventy articles and several courses (both university and commercial) on C and Unix. In addition to programming and consulting, Ian teaches Unix, C, and Java for Learning Tree International, one of the world's largest technical training companies. He runs OpenBSD on most of his computers, and he runs a mirror of The Unix History Society archive.

Table of Contents

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

Customer Reviews

Most Helpful Customer Reviews

See All Customer Reviews

Java Cookbook, Second Edition 4.5 out of 5 based on 0 ratings. 2 reviews.
Guest More than 1 year ago
This is an excellent book¿it manages to make itself appropriate for both introductory and experienced Java developers. If you¿ve ever looked at any of the books in O¿Reilly¿s ¿Cookbook¿ series, you¿ll know that the book is organized into ¿recipes¿, which illustrate how to accomplish various development tasks in Java. Many of the beginning recipes are pretty basic, but will be used by developers new to Java to get started. Recipes include setting up the IDE to work correctly, working with a debugger, and even an introduction to JUnit. The next few sections of recipes are similar to what you would expect from any ¿Learn to Program in Language¿ book. There is a discussion on strings and string use, numbers, and date time values. As the book progresses, the recipes become increasingly technical. I was quite impressed to see a discussion of Generics in chapter 8, which were added to 1.5. Additional technical recipes include I/O, graphics and sound, and using sockets in Java. The author is able to successfully discuss a lot of different topics in a very clear and concise way. Additionally, I was both pleased and surprised to see the author include brief discussions on software patterns and agile methodologies, both of which are very much coming into vogue. Naturally, this book isn¿t easy to read from cover to cover, but it was never designed that way. Much like an actual cookbook, it is designed to best be used as a reference manual. Experienced non-Java programmers will benefit from this book, which can be used as a good tutorial to learn the Java language quickly. Java programmers will also benefit greatly from this book, and will likely want to have it next to their computer as a reference manual. I would highly recommend this book to anyone either wanting to learn Java or anyone looking for a good Java reference book.
Guest More than 1 year ago
[A review of the SECOND EDITION, 2004] The salient feature that distinguishes this second edition from its predecessor is the coverage of Java 1.5. The overall format of the book is unchanged. There are over 100 'hacks' that address common problems a Java programmer might face. The grouping of hacks into chapters is quite logical. But you are expected to already know the basics of Java programming. This book is not meant to teach that, but to help fill in gaps in your overall knowledge framework. The solutions are typically easy to understand. That is the tenor of the book. You can quickly see if a solution fits your needs and then easily apply it. The 1.5 features are sprinkled throughout the chapters. Because the numerous changes from 1.4 are distributed over many aspects of the language. So Darwin correctly chose not to aggregate these into one location. Which also means that this book is not the place to learn specifically about 1.5 as a whole. (Try 'Java 1.5 Tiger' by McLaughlin and Flanagan.)