Java Cookbook, 2/e (Paperback)
暫譯: Java 食譜,第 2 版 (平裝本)

Ian F Darwin

  • 出版商: O'Reilly
  • 出版日期: 2004-06-24
  • 售價: $1,880
  • 貴賓價: 9.5$1,786
  • 語言: 英文
  • 頁數: 864
  • 裝訂: Paperback
  • ISBN: 0596007019
  • ISBN-13: 9780596007010
  • 相關分類: Java 程式語言
  • 已過版

買這商品的人也買了...

相關主題

商品描述

Description:

You have a choice: you can wade your way through lengthy Java tutorials and figure things out by trial and error, or you can pick up Java Cookbook, 2nd Edition and get to the heart of what you need to know when you need to know it.

With the completely revised and thoroughly updated Java Cookbook, 2nd Edition, Java developers like you will learn by example, try out new features, and use sample code to understand how new additions to the language and platform work--and how to put them to work for you.

This comprehensive collection of problems, solutions, and practical examples will satisfy Java developers at all levels of expertise. Whether you're new to Java programming and need something to bridge the gap between theory-laden reference manuals and real-world programs or you're a seasoned Java programmer looking for a new perspective or a different problem-solving context, this book will help you make the most of your Java knowledge.

Packed with hundreds of tried-and-true Java recipes covering all of the major APIs from the 1.4 version of Java, this book also offers significant first-look recipes for the most important features of the new 1.5 version, which is in beta release. You get practical solutions to everyday problems, and each is followed by a detailed, ultimately useful explanation of how and why the technology works.

Java Cookbook, 2nd Edition includes code segments covering many specialized APIs--like those for working with Struts, Ant and other new popular Open Source tools. It also includes expanded Mac OS X Panther coverage and serves as a great launching point for Java developers who want to get started in areas outside of their specialization.

In this major revision, you'll find succinct pieces of code that can be easily incorporated into other programs. Focusing on what's useful or tricky--or what's useful and tricky--Java Cookbook, 2nd Edition is the most practical Java programming book on the market.

Table of Contents:

Preface

1. Getting Started: Compiling, Running, and Debugging

      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

2. Interacting with the Environment

      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

3. Strings and Things

      3.1 Taking Strings Apart with Substrings

      3.2 Taking Strings Apart with StringTokenizer

      3.3

       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

4. Pattern Matching with Regular Expressions

      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

5. Numbers

      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

6. Dates and Times

      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 Date or 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

7. Structuring Data with Java

      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

8. Data Structuring with Generics, foreach, and Enumerations (JDK 1.5)

      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

9. Object-Oriented Techniques

      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

10. Input and Output

      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

11. Directory and Filesystem Operations

      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

12. Programming External Devices: Serial and Parallel Ports

      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

13. Graphics and Sound

      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

14. Graphical User Interfaces

      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

15. Internationalization and Localization

      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

16. Network Clients

      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

17. Server-Side Java: Sockets

      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

18. Network Clients II: Applets and Web Clients

      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

19. Java and Electronic Mail

      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

20. Database Access

      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

21. XML

      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

22. Distributed Java: RMI

      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

23. Packages and Packaging

      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

24. Threaded Java

      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)

25. Introspection, or "A Class Named Class"

      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

26. Using Java with Other Languages

      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

Afterword

Index

商品描述(中文翻譯)

**描述:**

您有兩個選擇:您可以在冗長的 Java 教學中摸索,通過反覆試驗來了解事物,或者您可以拿起《Java Cookbook》第 2 版,直接掌握您需要知道的核心知識。

隨著《Java Cookbook》第 2 版的全面修訂和徹底更新,像您這樣的 Java 開發者將通過範例學習,嘗試新功能,並使用示例代碼來理解語言和平台的新增功能如何運作,以及如何將它們應用於您的工作。

這本全面的問題、解決方案和實用範例的集合將滿足各個技術水平的 Java 開發者。無論您是剛接觸 Java 編程,需要一些東西來填補理論參考手冊與實際程序之間的差距,還是您是一位經驗豐富的 Java 程序員,尋找新的視角或不同的問題解決背景,這本書都將幫助您充分利用您的 Java 知識。

本書包含數百個經過驗證的 Java 食譜,涵蓋了 Java 1.4 版本的所有主要 API,還提供了對新 1.5 版本最重要功能的顯著首發食譜,該版本目前處於測試階段。您將獲得日常問題的實用解決方案,每個解決方案後面都有詳細且最終有用的解釋,說明技術如何運作以及為什麼這樣運作。

《Java Cookbook》第 2 版包括涵蓋許多專用 API 的代碼片段,例如用於處理 Struts、Ant 和其他新流行的開源工具的 API。它還擴展了對 Mac OS X Panther 的覆蓋,並為希望在其專業領域之外開始探索的 Java 開發者提供了良好的起點。

在這次重大修訂中,您將發現簡潔的代碼片段,可以輕鬆地整合到其他程序中。專注於有用或棘手的內容,或是同時具備有用和棘手的內容,《Java Cookbook》第 2 版是市場上最實用的 Java 編程書籍。

**目錄:**

**前言**

**1. 開始使用:編譯、運行和調試**
      1.1 編譯和運行 Java:JDK
      1.2 使用顏色高亮編輯器進行編輯和編譯
      1.3 使用 IDE 進行編譯、運行和測試
      1.4 有效使用 CLASSPATH
      1.5 使用本書中的 com.darwinsys API 類
      1.6 編譯本書中的源代碼示例
      1.7 使用 Ant 自動化編譯
      1.8 運行 Applet
      1.9 處理棄用警告
      1.10 無需 #ifdef 的條件調試
      1.11 調試輸出
      1.12 使用斷言維護程序正確性
      1.13 使用 JDB 進行調試
      1.14 單元測試:避免需要調試器
      1.15 獲取可讀的追蹤信息
      1.16 查找更多 Java 源代碼
      1.17 程序:調試

**2. 與環境互動**
      2.1 獲取環境變量
      2.2 系統屬性
      2.3 編寫依賴於 JDK 版本的代碼
      2.4 編寫依賴於操作系統的代碼
      2.5 使用擴展或其他打包 API
      2.6 解析命令行參數

**3. 字串與其他**
      3.1 使用子字串拆分字串
      3.2 使用 StringTokenizer 拆分字串
      3.3
        StringBuffer
      3.4 一次處理一個字元的字串
      3.5 對齊字串
      3.6 在 Unicode 字元和字串之間轉換
      3.7 按字或字元反轉字串
      3.8 展開和壓縮 Tab
      3.9 控制大小寫
      3.10 縮排文本文件
      3.11 輸入不可列印字元
      3.12 剪裁字串末尾的空白
      3.13 解析以逗號分隔的數據
      3.14 程序:簡單的文本格式化器
      3.15 程序:Soundex 名稱比較

**4. 使用正則表達式進行模式匹配**
      4.1 正則表達式語法
      4.2 在 Java 中使用正則表達式:測試模式
      4.3 查找匹配的文本
      4.4 替換匹配的文本
      4.5 打印模式的所有出現
      4.6 打印包含模式的行
      4.7 在正則表達式中控制大小寫
      4.8 匹配「重音」或複合字元
      4.9 匹配文本中的換行符
      4.10 程序:Apache 日誌文件解析
      4.11 程序:數據挖掘
      4.12 程序:完整的 Grep

**5. 數字**
      5.1 檢查字串是否為有效數字
      5.2 在較小的數字中存儲較大的數字
      5.3 將數字轉換為對象及其反向操作
      5.4 在不使用浮點數的情況下取整數的分數
      5.5 確保浮點數的準確性
      5.6 比較浮點數
      5.7 四捨五入浮點數
      5.8 格式化數字
      5.9 在二進制、八進制、十進制和十六進制之間轉換
      5.10 對一系列整數進行操作
      5.11 處理羅馬數字
      5.12 使用正確的複數進行格式化
      5.13 生成隨機數字
      5.14 生成更好的隨機數字
      5.15 計算三角函數
      5.16 取對數
      5.17 矩陣相乘
      5.18 使用複數
      5.19 處理非常大的數字
      5.20 程序:TempConverter
      5.21 程序:數字回文

**6. 日期和時間**
      6.1 查找今天的日期
      6.2 以給定格式打印日期/時間
      6.3 在其他時期表示日期
      6.4 將 YMDHMS 轉換為日曆或時期秒
      6.5 將字串解析為日期
      6.6 將時期秒轉換為 DMYHMS
      6.7 對日期或日曆進行加減
      6.8 計算兩個日期之間的差異
      6.9 比較日期
      6.10 星期/月份/年份或週數
      6.11 創建日曆頁面
      6.12 測量經過的時間
      6.13 暫停一段時間
      6.14 程序:提醒服務

**7. 使用 Java 結構化數據**
      7.1 使用數組進行數據結構化
      7.2 調整數組大小
      7.3 像數組,但更具動態性
      7.4 使用迭代器進行數據獨立訪問
      7.5 在鏈表中結構化數據
      7.6 使用 Hashtable 和 HashMap 進行映射
      7.7 在屬性和偏好中存儲字串
      7.8 對集合進行排序
      7.9 避免排序的衝動
      7.10 避免重複
      7.11 在集合中查找對象
      7.12 將集合轉換為數組
      7.13 自行實現迭代器
      7.14 堆疊
      7.15 多維結構
      7.16 最後,集合
      7.17 程序:計時比較

**8. 使用泛型、foreach 和枚舉進行數據結構化(JDK 1.5)**
      8.1 使用泛型集合
      8.2 使用 'foreach' 循環
      8.3 通過使用泛型避免類型轉換
      8.4 讓 Java 通過自動裝箱和自動拆箱進行轉換
      8.5 使用類型安全的枚舉
      8.6 程序:MediaInvoicer

**9. 面向對象技術**
      9.1 打印對象:使用 toString() 格式化
      9.2 重寫 equals 方法
      9.3 重寫 hashCode 方法
      9.4 clone 方法
      9.5 finalize 方法
      9.6 使用內部類
      9.7 通過接口提供回調
      9.8 多態/抽象方法
      9.9 傳遞值
      9.10 強制執行單例模式
      9.11 自行實現異常
      9.12 程序:Plotter

**10. 輸入和輸出**
      10.1 讀取標準輸入
      10.2 寫入標準輸出
      10.3 使用 1.5 格式化器進行打印
      10.4 使用 StreamTokenizer 扫描文件
      10.5 使用 1.5 Scanner 類掃描輸入
      10.6 按名稱打開文件
      10.7 複製文件
      10.8 將文件讀入字串
      10.9 重新分配標準流
      10.10 在寫入時複製流
      10.11 讀取/寫入不同的字符集
      10.12 那些煩人的行結束字符
      10.13 注意平台依賴的文件代碼
      10.14 讀取「續行」
      10.15 二進制數據
      10.16 尋找
      10.17 從 C 寫入數據流
      10.18 保存和恢復 Java 對象
      10.19 使用 SerialVersionUID 防止 ClassCastExceptions