首页 > > > [Java.Cookbook(2nd,2004.6)].Ian.F.Darwin.文字版.pdf

[Java.Cookbook(2nd,2004.6)].Ian.F.Darwin.文字版.pdf

[Java.Cookbook(2nd,2004.6)].Ian…

上传者: 二二二二二二二2 2014-02-22 评分1 评论0 下载1 收藏10 阅读量878 暂无简介 简介 举报

简介:本文档为《[Java.Cookbook(2nd,2004.6)].Ian.F.Darwin.文字版pdf》,可适用于软件工程领域,主题内容包含JavaCookbookOtherJavaresourcesfromO’ReillyRelatedtitlesJavainaNutshellHead符等。

Java Cookbook Other Java resources from O’Reilly Related titles Java in a Nutshell Head First Java Head First EJB Programming Jakarta Struts Tomcat: The Definitive Guide Learning Java Better, Faster, Lighter Java Java Servlet and JSP Cookbook Hardcore Java JavaServer Pages Java Books Resource Center java.oreilly.com is a complete catalog of O’Reilly’s books on Java and related technologies, including sample chapters and code examples. OnJava.com is a one-stop resource for enterprise Java develop- ers, featuring news, code recipes, interviews, weblogs, and more. Conferences O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in document- ing the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches. Visit con- ferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online refer- ence library for programmers and IT professionals. Conduct searches across more than 1,000 books. Subscribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or sim- ply flip to the page you need. Try it today with a free trial. Java Cookbook SECOND EDITION Ian F. Darwin Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo Java Cookbook, Second Edition by Ian F. Darwin Copyright 2004, 2001 O’Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editors: Mike Loukides and Debra Cameron Production Editor: Marlowe Shaeffer Cover Designer: Hanna Dyer Interior Designer: David Futato Printing History: June 2001: First Edition. June 2004: Second Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. The Cookbook series designations, Java Cookbook, the image of a domestic chicken, and related trade dress are trademarks of O’Reilly Media, Inc. Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc., in the United States and other countries. O’Reilly Media, Inc. is independent of Sun Microsystems, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. This book uses RepKover, a durable and flexible lay-flat binding. ISBN: 0-596-00701-9 ISBN13: 978-0-596-00701-0 [M] [1/07] v Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv 1. Getting Started: Compiling, Running, and Debugging . . . . . . . . . . . . . . . . . . . 1 1.1 Compiling and Running Java: JDK 1 1.2 Editing and Compiling with a Color-Highlighting Editor 3 1.3 Compiling, Running, and Testing with an IDE 4 1.4 Using CLASSPATH Effectively 11 1.5 Using the com.darwinsys API Classes from This Book 14 1.6 Compiling the Source Code Examples from This Book 15 1.7 Automating Compilation with Ant 16 1.8 Running Applets 17 1.9 Dealing with Deprecation Warnings 20 1.10 Conditional Debugging Without #ifdef 22 1.11 Debugging Printouts 24 1.12 Maintaining Program Correctness with Assertions 25 1.13 Debugging with JDB 26 1.14 Unit Testing: Avoid the Need for Debuggers 28 1.15 Getting Readable Tracebacks 30 1.16 Finding More Java Source Code 32 1.17 Program: Debug 33 2. Interacting with the Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2.1 Getting Environment Variables 35 2.2 System Properties 37 2.3 Writing JDK Release-Dependent Code 39 2.4 Writing Operating System-Dependent Code 40 2.5 Using Extensions or Other Packaged APIs 42 2.6 Parsing Command-Line Arguments 43 vi | Table of Contents 3. Strings and Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 3.1 Taking Strings Apart with Substrings 52 3.2 Taking Strings Apart with StringTokenizer 53 3.3 Putting Strings Together with +, StringBuilder (JDK 1.5), and StringBuffer 56 3.4 Processing a String One Character at a Time 59 3.5 Aligning Strings 60 3.6 Converting Between Unicode Characters and Strings 63 3.7 Reversing a String by Word or by Character 64 3.8 Expanding and Compressing Tabs 65 3.9 Controlling Case 70 3.10 Indenting Text Documents 71 3.11 Entering Nonprintable Characters 73 3.12 Trimming Blanks from the End of a String 74 3.13 Parsing Comma-Separated Data 75 3.14 Program: A Simple Text Formatter 80 3.15 Program: Soundex Name Comparisons 82 4. Pattern Matching with Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 4.1 Regular Expression Syntax 87 4.2 Using regexes in Java: Test for a Pattern 94 4.3 Finding the Matching Text 97 4.4 Replacing the Matched Text 99 4.5 Printing All Occurrences of a Pattern 100 4.6 Printing Lines Containing a Pattern 103 4.7 Controlling Case in Regular Expressions 104 4.8 Matching “Accented” or Composite Characters 105 4.9 Matching Newlines in Text 106 4.10 Program: Apache Logfile Parsing 108 4.11 Program: Data Mining 110 4.12 Program: Full Grep 112 5. Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 5.1 Checking Whether a String Is a Valid Number 119 5.2 Storing a Larger Number in a Smaller Number 120 5.3 Converting Numbers to Objects and Vice Versa 121 5.4 Taking a Fraction of an Integer Without Using Floating Point 122 5.5 Ensuring the Accuracy of Floating-Point Numbers 123 5.6 Comparing Floating-Point Numbers 125 Table of Contents | vii 5.7 Rounding Floating-Point Numbers 127 5.8 Formatting Numbers 128 5.9 Converting Between Binary, Octal, Decimal, and Hexadecimal 130 5.10 Operating on a Series of Integers 131 5.11 Working with Roman Numerals 132 5.12 Formatting with Correct Plurals 136 5.13 Generating Random Numbers 138 5.14 Generating Better Random Numbers 139 5.15 Calculating Trigonometric Functions 140 5.16 Taking Logarithms 141 5.17 Multiplying Matrices 141 5.18 Using Complex Numbers 143 5.19 Handling Very Large Numbers 145 5.20 Program: TempConverter 147 5.21 Program: Number Palindromes 151 6. Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 6.1 Finding Today’s Date 155 6.2 Printing Date/Time in a Given Format 156 6.3 Representing Dates in Other Epochs 159 6.4 Converting YMDHMS to a Calendar or Epoch Seconds 160 6.5 Parsing Strings into Dates 161 6.6 Converting Epoch Seconds to DMYHMS 162 6.7 Adding to or Subtracting from a Date or Calendar 163 6.8 Difference Between Two Dates 165 6.9 Comparing Dates 165 6.10 Day of Week/Month/Year or Week Number 167 6.11 Creating a Calendar Page 168 6.12 Measuring Elapsed Time 171 6.13 Sleeping for a While 173 6.14 Program: Reminder Service 173 7. Structuring Data with Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 7.1 Using Arrays for Data Structuring 177 7.2 Resizing an Array 178 7.3 Like an Array, but More Dynamic 180 7.4 Using Iterators for Data-Independent Access 181 7.5 Structuring Data in a Linked List 183 7.6 Mapping with Hashtable and HashMap 185 viii | Table of Contents 7.7 Storing Strings in Properties and Preferences 186 7.8 Sorting a Collection 190 7.9 Avoiding the Urge to Sort 193 7.10 Eschewing Duplication 195 7.11 Finding an Object in a Collection 196 7.12 Converting a Collection to an Array 198 7.13 Rolling Your Own Iterator 199 7.14 Stack 201 7.15 Multidimensional Structures 202 7.16 Finally, Collections 204 7.17 Program: Timing Comparisons 206 8. Data Structuring with Generics, foreach, and Enumerations (JDK 1.5) . . . 208 8.1 Using Generic Collections 209 8.2 Using “foreach” Loops 210 8.3 Avoid Casting by Using Generics 211 8.4 Let Java Convert with AutoBoxing and AutoUnboxing 214 8.5 Using Typesafe Enumerations 215 8.6 Program: MediaInvoicer 219 9. Object-Oriented Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 9.1 Printing Objects: Formatting with toString( ) 224 9.2 Overriding the Equals Method 225 9.3 Overriding the hashCode Method 228 9.4 The Clone Method 229 9.5 The Finalize Method 231 9.6 Using Inner Classes 233 9.7 Providing Callbacks via Interfaces 234 9.8 Polymorphism/Abstract Methods 238 9.9 Passing Values 239 9.10 Enforcing the Singleton Pattern 242 9.11 Roll Your Own Exceptions 243 9.12 Program: Plotter 244 10. Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 10.1 Reading Standard Input 248 10.2 Writing Standard Output 252 10.3 Printing with the 1.5 Formatter 253 10.4 Scanning a File with StreamTokenizer 257 10.5 Scanning Input with the 1.5 Scanner Class 262 Table of Contents | ix 10.6 Opening a File by Name 265 10.7 Copying a File 266 10.8 Reading a File into a String 269 10.9 Reassigning the Standard Streams 270 10.10 Duplicating a Stream as It Is Written 270 10.11 Reading/Writing a Different Character Set 273 10.12 Those Pesky End-of-Line Characters 274 10.13 Beware Platform-Dependent File Code 274 10.14 Reading “Continued” Lines 275 10.15 Binary Data 280 10.16 Seeking 281 10.17 Writing Data Streams from C 282 10.18 Saving and Restoring Java Objects 284 10.19 Preventing ClassCastExceptions with SerialVersionUID 287 10.20 Reading and Writing JAR or Zip Archives 289 10.21 Reading and Writing Compressed Files 292 10.22 Program: Text to PostScript 293 11. Directory and Filesystem Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 11.1 Getting File Information 297 11.2 Creating a File 300 11.3 Renaming a File 301 11.4 Deleting a File 302 11.5 Creating a Transient File 303 11.6 Changing File Attributes 305 11.7 Listing a Directory 306 11.8 Getting the Directory Roots 308 11.9 Creating New Directories 309 11.10 Program: Find 310 12. Programming External Devices: Serial and Parallel Ports . . . . . . . . . . . . . . 313 12.1 Choosing a Port 315 12.2 Opening a Serial Port 318 12.3 Opening a Parallel Port 322 12.4 Resolving Port Conflicts 325 12.5 Reading and Writing: Lock-Step 328 12.6 Reading and Writing: Event-Driven 331 12.7 Reading and Writing: Threads 335 12.8 Program: Penman Plotter 337 x | Table of Contents 13. Graphics and Sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 13.1 Painting with a Graphics Object 342 13.2 Testing Graphical Components 344 13.3 Drawing Text 344 13.4 Drawing Centered Text in a Component 345 13.5 Drawing a Drop Shadow 347 13.6 Drawing Text with 2D 349 13.7 Drawing Text with an Application Font 352 13.8 Drawing an Image 354 13.9 Playing a Sound File 358 13.10 Playing a Video Clip 360 13.11 Printing in Java 362 13.12 Program: PlotterAWT 366 13.13 Program: Grapher 368 14. Graphical User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372 14.1 Displaying GUI Components 373 14.2 Designing a Window Layout 375 14.3 A Tabbed View of Life 378 14.4 Action Handling: Making Buttons Work 379 14.5 Action Handling Using Anonymous Inner Classes 381 14.6 Terminating a Program with “Window Close” 383 14.7 Dialogs: When Later Just Won’t Do 387 14.8 Catching and Formatting GUI Exceptions 389 14.9 Getting Program Output into a Window 391 14.10 Choosing a Value with JSpinner 395 14.11 Choosing a File with JFileChooser 396 14.12 Choosing a Color 399 14.13 Formatting JComponents with HTML 402 14.14 Centering a Main Window 403 14.15 Changing a Swing Program’s Look and Feel 404 14.16 Enhancing Your GUI for Mac OS X 408 14.17 Program: Custom Font Chooser 410 14.18 Program: Custom Layout Manager 414 15. Internationalization and Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421 15.1 Creating a Button with I18N Resources 422 15.2 Listing Available Locales 423 15.3 Creating a Menu with I18N Resources 424 Table of Contents | xi 15.4 Writing Internationalization Convenience Routines 425 15.5 Creating a Dialog with I18N Resources 427 15.6 Creating a Resource Bundle 428 15.7 Extracting Strings from Your Code 429 15.8 Using a Particular Locale 430 15.9 Setting the Default Locale 431 15.10 Formatting Messages 432 15.11 Program: MenuIntl 434 15.12 Program: BusCard 436 16. Network Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 16.1 Contacting a Server 443 16.2 Finding and Reporting Network Addresses 444 16.3 Handling Network Errors 446 16.4 Reading and Writing Textual Data 447 16.5 Reading and Writing Binary Data 449 16.6 Reading and Writing Serialized Data 451 16.7 UDP Datagrams 453 16.8 Program: TFTP UDP Client 455 16.9 Program: Telnet Client 459 16.10 Program: Chat Client 461 17. Server-Side Java: Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467 17.1 Opening a Server for Business 467 17.2 Returning a Response (String or Binary) 470 17.3 Returning Object Information 474 17.4 Handling Multiple Clients 475 17.5 Serving the HTTP Protocol 480 17.6 Securing a Web Server with SSL and JSSE 482 17.7 Network Logging 484 17.8 Network Logging with log4j 489 17.9 Network Logging with JDK 1.4 491 17.10 Finding Network Interfaces 493 17.11 Program: A Java Chat Server 495 18. Network Clients II: Applets and Web Clients . . . . . . . . . . . . . . . . . . . . . . . . . . 501 18.1 Embedding Java in a Web Page 501 18.2 Applet Techniques 503 18.3 Contacting a Server on the Applet Host 505 xii | Table of Contents 18.4 Making an Applet Show a Document 508 18.5 Making an Applet Run JavaScript 510 18.6 Making an Applet Run a CGI Script 511 18.7 Reading the Contents of a URL 512 18.8 URI, URL, or URN? 513 18.9 Extracting HTML from a URL 515 18.10 Extracting URLs from a File 517 18.11 Converting a Filename to a URL 519 18.12 Program: MkIndex 519 18.13 Program: LinkChecker 524 19. Java and Electronic Mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531 19.1 Sending Email: Browser Version 531 19.2 Sending Email: For Real 535 19.3 Mail-Enabling a Server Program 539 19.4 Sending MIME Mail 543 19.5 Providing Mail Settings 545 19.6 Sending Mail Without Using JavaMail 546 19.7 Reading Email 550 19.8 Program: MailReaderBean 555 19.9 Program: MailClient 559 20. Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570 20.1 Easy Database Access with JDO 571 20.2 Text-File Databases 574 20.3 DBM Databases 579 20.4 JDBC Setup and Connection 582 20.5 Connecting to a JDBC Database 585 20.6 Sending a JDBC Query and Getting Results 588 20.7 Using JDBC Prepared Statements 590 20.8 Using Stored Procedures with JDBC 594 20.9 Changing Data Using a ResultSet 595 20.10 Storing Results in a RowSet 596 20.11 Changing Data Using SQL 598 20.12 Finding JDBC Metadata 600 20.13 Program: SQLRunner 604 21. XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615 21.1 Generating XML from Objects 618 21.2 Transforming XML with XSLT 619 Table of Contents | xiii 21.3 Parsing XML with SAX 622 21.4 Parsing XML with DOM 624 21.5 Verifying Structure with a DTD 628 21.6 Generating Your Own XML with DOM 630 21.7 Program: xml2mif 632 22. Distributed Java: RMI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634 22.1 Defining the RMI Contract 635 22.2 Creating an RMI Client 637 22.3 Creating an RMI Server 638 22.4 Deploying RMI Across a Network 641 22.5 Program: RMI Callbacks 641 22.6 Program: NetWatch 646 23. Packages and Packaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652 23.1 Creating a Package 653 23.2 Documenting Classes with Javadoc 653 23.3 Beyond JavaDoc: Annotations/Metadata (JDK 1.5) and XDoclet 657 23.4 Archiving with jar 660 23.5 Running an Applet from a JAR 661 23.6 Running an Applet with a Modern JDK 661 23.7 Running a Main Program from a JAR 665 23.8 Preparing a Class as a JavaBean 667 23.9 Pickling Your Bean into a JAR 671 23.10 Packaging a Servlet into a WAR File 672 23.11 “Write Once, Install Anywhere” 673 23.12 “Write Once, Install on Mac OS X” 673 23.13 Java Web Start 675 23.14 Signing Your JAR File 681 24. Threaded Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683 24.1 Running Code in a Different Thread 685 24.2 Displaying a Moving Image with Animation 688 24.3 Stopping a Thread 692 24.4 Rendezvous and Timeouts 694 24.5 Synchronizing Threads with the synchronized Keyword 695 24.6 Simplifying Synchronization with 1.5 Locks 701 24.7 Synchronizing Threads with wait() and notifyAll() 705 24.8 Simplifying Producer-Consumer with the 1.5 Queue Interface 711 xiv | Table of Contents 24.9 Background Saving in an Editor 713 24.10 Program: Threaded Network Server 714 24.11 Simplifying Servers Using the Concurrency Utilities (JDK 1.5) 722 25. Introspection, or “A Class Named Class” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725 25.1 Getting a Class Descriptor 726 25.2 Finding and Using Methods and Fields 727 25.3 Loading and Instantiating a Class Dynamically 731 25.4 Constructing a Class from Scratch 733 25.5 Performance Timing 734 25.6 Printing Class Information 737 25.7 Program: CrossRef 739 25.8 Program: AppletViewer 745 26. Using Java with Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752 26.1 Running a Program 752 26.2 Running a Program and Capturing Its Output 755 26.3 Mixing Java and Scripts with BSF 759 26.4 Marrying Java and Perl 763 26.5 Blending in Native Code (C/C++) 767 26.6 Calling Java from Native Code 773 26.7 Program: DBM 773 Afterword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779 This is the Title of the Book, eMatter Edition Copyright 2007 O’Reilly & Associates, Inc. All rights reserved. xv Preface Preface to the Second Edition JDK 1.5, code-named Tiger, is an exciting change to the Java landscape. It intro- duces several major new facilities, such as generic types for better data structuring, metadata for annotating Java classes in a flexible but well-defined manner, new pattern-based mechanisms for reading data, and a new mechanism for formatted printing. In addition, a much larger number of smaller but important changes add up to a new release that is a must for Java developers. It will be quite some time before these mechanisms are fully understood and in wide circulation, but you will want to know about them right away. I wrote in the Afterword to the first edition that “writing this book has been a hum- bling experience.” I should add that maintaining it has been humbling, too. While many reviewers and writers have

该用户的其他资料

  • 名称/格式
  • 评分
  • 下载次数
  • 资料大小
  • 上传时间

用户评论

0/200
    暂无评论
上传我的资料

相关资料

资料评价:

/ 864
所需积分:1 立即下载
返回
顶部
举报
资料
关闭

温馨提示

感谢您对爱问共享资料的支持,精彩活动将尽快为您呈现,敬请期待!