You are previewing Java Programming.

Java Programming

Cover of Java Programming by Poornachandra Sarang Published by McGraw-Hill, Oracle Press
  1. Cover 
  2. Half title
  3. About the Author
  4. Title
  5. Copyright
  6. Dedication
  7. Contents at a Glance
  8. Contents 
  9. Foreword
  10. Acknowledgments
  11. Introduction
  12. 1: Introduction to Java
    1. Why Java?
    2. So What Is Java?
    3. Java Virtual Machine
    4. Features of Java
      1. Small
      2. Simple
      3. Object Oriented
      4. Compiled and Interpreted
      5. Platform Independent
      6. Robust and Secure
      7. Multithreaded
      8. Dynamic
    5. Java’s Evolution
      1. JDK 1.0 (January 23, 1996): Codename Oak
      2. JDK 1.1 (February 19, 1997)
      3. J2SE 1.2 (December 8, 1998): Codename Playground
      4. J2SE 1.3 (May 8, 2000): Codename Kestrel
      5. J2SE 1.4 (Feb 6, 2002): Codename Merlin
      6. J2SE 5.0 (Sept 30, 2004): Codename Tiger
      7. Java SE 6 (Dec 11, 2006): Codename Mustang
      8. Java SE 7 (July 7, 2011): Codename Dolphin
    6. Summary
  13. 2: Arrays
    1. Arrays
      1. Declaring Arrays
      2. Creating Arrays
      3. Accessing and Modifying Array Elements
    2. Initializing Arrays
      1. Initializing at Runtime
      2. Initializing Using Array Literals
    3. The for-each Loop
    4. Multidimensional Arrays
      1. Two-dimensional Arrays
      2. Initializing Two-dimensional Arrays
      3. Looping Using the for-each Construct
    5. N-dimensional Arrays
    6. Nonrectangular Arrays
      1. Runtime Initialization
      2. Initialization Using Array Literals
    7. A Few Goodies
      1. Determining the Array Length
      2. Cloning an Array
      3. Finding Out the Class of an Array
    8. Summary
  14. 3: Classes
    1. Object-Oriented Programming (OOP) Concepts
      1. OOP Features
      2. OOP Benefits
    2. The Class
      1. Defining a Class
      2. Declaring a Point Class
      3. Using Classes
      4. Accessing/Modifying Fields
      5. The Class Example Program
      6. Declaring Methods
      7. Memory Representation of Objects
    3. Information Hiding
    4. Encapsulation
    5. Declaring Constructors
      1. Default Constructor
      2. Rules for Defining a Constructor
    6. Source File Layout
      1. The package Statement
      2. The import Statement
    7. Directory Layout and Packages
    8. Summary
  15. 4: Inheritance
    1. Why Inheritance?
    2. What Is Inheritance?
    3. Defining Single-level Inheritance
      1. Capturing Multilevel Inheritance
      2. Writing a Multilevel Inheritance Program
    4. Polymorphism
      1. Creating a Heterogeneous Collection of Objects
      2. A Program That Demonstrates a Heterogeneous Collection
      3. Detecting the Object Type
      4. Typecasting Rules on Inheritance Hierarchies
      5. Preventing Method Overriding
      6. Preventing Subclassing
    5. Summary
  16. 5: Object Creation and Member Visibility
    1. Instantiating a Subclass
      1. The Object-Creation Process
      2. Calling the super Constructor
      3. Method Overloading
      4. Rules of Method Overloading
    2. Creating a Copy Constructor
      1. Invoking Constructors: Summary
    3. The final Keyword
      1. The final Classes
      2. The final Methods
      3. The final Variables
      4. The final Variables of the Class Type
      5. Important Points Related to the final Keyword
    4. Understanding Member Visibility Rules
      1. The public Modifier
      2. The private Modifier
      3. The protected Modifier
      4. The Default Modifier
      5. A Few Rules on Inheriting
    5. Summary
  17. 6: Static Modifier and Interfaces
    1. The static Keyword
      1. The Static Fields
      2. The Static Methods
      3. The Static Initializers
    2. Interfaces
      1. A Real-life Example of an Interface
      2. Understanding Interface Syntax
      3. Understanding Interfaces Through an Example
      4. Extending Interfaces
      5. Implementing Multiple Interfaces
      6. Combining Interfaces
      7. A Few Important Points on Interfaces
    3. Abstract Classes
    4. Summary
  18. 7: Nested Classes
    1. Nested Classes
      1. Why Use Nested Classes?
      2. Classifications of Nested Classes
      3. Demonstrating the Use of Inner Classes
      4. Accessing an Inner Class from the Outside
      5. Accessing Shadowed Variables
      6. Important Points to Note
    2. Member Classes
      1. Local Classes
      2. Defining an Inner Class within Method Scope
      3. A Few Important Points on Local Classes
    3. Anonymous Classes
      1. Creating Anonymous Classes
      2. Restrictions on the Use of Anonymous Classes
      3. Compiled Anonymous Classes
      4. Guidelines on Using Anonymous Classes
    4. Summary
  19. 8: Exception Handling
    1. What Is an Exception?
      1. Error Types
      2. The Non-fatal Errors
      3. The try-catch Statements
      4. Classifying Exceptions
      5. Combining Exception Handlers
      6. How Runtime Matches catch Blocks
    2. The finally Statement
      1. Guidelines on the Use of the finally Block
      2. Rules for Using the try/catch/finally Block
      3. The try-with-resources Statement
    3. Checked/Unchecked Exceptions
      1. The throws Construct
      2. Throwing Multiple Exceptions
    4. User-defined Exceptions
      1. The throw Statement
      2. Re-throwing Exceptions
      3. Difference Between the throw and throws Keywords
      4. The final Re-throw in Java SE 7
    5. Declaring Exceptions in Overridden Methods
    6. Printing a Stack Trace
    7. Asynchronous Exceptions
    8. Guidelines for Using Exceptions
    9. Summary
  20. 9: Java I/O
    1. Input/Output Streams
    2. The I/O Class Hierarchy
      1. The Byte Streams
      2. Determining File Length
      3. The InputStream Methods
      4. The OutputStream Class
      5. File Copy Utility
      6. The OutputStream Methods
    3. Character Streams
      1. File Viewer Utility
      2. Buffered Readers/Writers
      3. Binary Versus Character Streams
      4. Chaining Streams
      5. The Line Count Program
      6. File Concatenation
    4. Accessing the Host File System
      1. The Directory Listing Program
      2. Filtering the Directory Listing
    5. Reading/Writing Objects
    6. Summary
  21. 10: Advanced I/O
    1. The Byte-Oriented Stream Classes
      1. The PushbackInputStream Class
      2. The SequenceInputStream Class
      3. The PrintStream Class
    2. The Character-Oriented Stream Classes
      1. The CharArray Reader/Writer Classes
      2. The Console Class
      3. The StreamTokenizer Class
    3. The Object-Oriented Streams
      1. The Externalizable Interface
      2. Nested Objects Serialization
      3. Versioning Objects
    4. Summary
  22. 11: Enums, Autoboxing, and Annotations
    1. Typesafe Enumerations
      1. Creating Integer Patterns for Enumerations
      2. The enum Type
      3. Serializing enum Types
    2. Autoboxing
      1. Wrapper Classes
      2. A Few Additions in J2SE 5.0
      3. Autoboxing/Unboxing
    3. Annotations
      1. Built-in Annotations
      2. Declaring Annotations
      3. Annotating an Annotation
    4. Summary
  23. 12: Generics
    1. Generics
      1. What Are Generics?
      2. Why Do We Need Generics?
      3. A Sample Generics Program
      4. Type Safety
    2. Creating a Parameterized Stack Type
      1. Declaration Syntax
      2. A Generic Stack Class
      3. Examining Intermediate Code
      4. Testing the Stack Class
    3. Bounded Types
      1. Using Wildcards
      2. Bounded Wildcards
      3. Raw Types
    4. More on Generic Types
      1. Class with Two Generic Parameters
      2. Casting Types
      3. Comparing and Assigning Generic Types
      4. Generic Methods
      5. Declaring Generic Interfaces
    5. Restrictions in Generics
      1. Creating Arrays
      2. Instantiating Type Parameters
      3. Use of the static Keyword
    6. Summary
  24. 13: Event Processing and GUI Building
    1. Event Processing Model
    2. Delegation Event Model
      1. The Event Source
      2. The Event Listener
      3. Event Processing Sequence
      4. Registering on Multiple Event Sources
      5. Multiple Event Types
    3. Building a GUI
      1. Creating the User Interface
      2. Demonstrating the Button Control
      3. Demonstrating the Edit Control
      4. Demonstrating the List Box Control
    4. Summary
  25. 14: Creating Layouts
    1. Layout Managers
      1. Types of Layout Managers
      2. Building the GUI
      3. How Do Layout Managers Work?
    2. Using Layout Managers
      1. BorderLayout
      2. Using NetBeans to Build the GUI
      3. FlowLayout
      4. CardLayout
      5. GridLayout
      6. GridBagLayout
      7. BoxLayout
    3. Tabbed Dialog Box
    4. Advanced Layout Managers
    5. Summary
  26. 15: Graphics and User Gestures Processing
    1. What Is an Applet?
      1. Creating Your First Applet
      2. Running the Applet
      3. Using AppletViewer
    2. Understanding Applet Life-cycle Methods
      1. Processing Mouse Events
      2. Creating Popup Menus
      3. Customizing the Drawing Color
      4. Processing Keyboard Events
    3. Summary
  27. 16: Collections
    1. What Is the Java Collections Framework?
    2. Benefits of the Collections Framework
    3. What the Collections Framework Offers
    4. The Collections Framework Interfaces
    5. The Collections Framework Classes
      1. List
      2. Optional Operations of the List Interface
      3. Set
      4. Queue
      5. Map
    6. Algorithms
    7. Summary
  28. 17: Threads
    1. Processes and Threads
      1. Thread States
      2. JVM Threading Implementations
      3. Daemon Versus Non-Daemon Threads
    2. Creating Threads
      1. Creating Your First Threaded Application
      2. Creating Non-Daemon Threads
      3. Thread Class Constructors
      4. Static Methods of Thread
      5. Some Essential Operations on Thread
    3. Thread Synchronization
      1. Bucket Transfers
      2. Producer/Consumer Problem
      3. Object Locks
      4. When to Synchronize
      5. The Deadlock
      6. Solutions to Deadlock
    4. Summary
  29. 18: Blocking Queues and Synchronizers
    1. Blocking Queues
      1. Characteristics of Blocking Queues
      2. The BlockingQueue Interface
      3. Implementations of the BlockingQueue Interface
      4. Stock-trading System
      5. The LinkedTransferQueue Example
    2. Synchronizers
      1. Semaphores
      2. Barriers
      3. Countdown Latches
      4. Phaser
      5. Exchangers
    3. Summary
  30. 19 Callables, Futures, Executors, and Fork/Join
    1. Callables and Futures
      1. The Callable Interface
      2. The Future Interface
      3. How Callable and Future Work
      4. Using Callables in Parallelizing Large Tasks
      5. The FutureTask Class
      6. Creating Cancellable Tasks
    2. Executors
      1. Creating a Thread Pool for Scheduled Executions
      2. The ScheduledExecutorService Class
      3. Demonstrating Scheduled Task Execution
      4. Obtaining the Results of the First Completed Execution
      5. Demonstrating the ExecutorCompletionService Class
    3. Fork/Join Framework
      1. The ForkJoinPool Class
      2. The ForkJoinTask Class
      3. Sorting an Enormous Array of Floats
    4. Thread-safe Collections
    5. The ThreadLocalRandom Class
    6. Summary
  31. 20: Network Programming
    1. Networking
      1. Simple Home Page Reader
      2. The URL Class
      3. The URLConnection Class
      4. Webpage Reader
      5. The HttpCookie Class
      6. Spying for Cookies
    2. Echo Server Application
      1. Testing the Echo Server Application
    3. Serving Multiple Clients
      1. Serving Simultaneous Clients
      2. Running the EchoMultiServer Application
      3. Testing the EchoMultiServer Application
    4. Writing a File Storage Server Application
      1. A Cloud Storage Server
      2. A Cloud Store Client
      3. Testing the File Upload/Download Utility
    5. The InetAddress Class
    6. Broadcasting Messages
      1. Writing a Stock Quotes Server
      2. Writing the Stock Trader Client
      3. Running the Server and Client
      4. Support for SCTP
    7. Summary
  32. 21: Utility Classes
    1. The String Class
      1. A Few Important Methods
      2. Practical Demonstration of String Methods
      3. Comparing Strings
      4. Creating Formatted Output
    2. The Calendar Class
      1. The GregorianCalendar Methods
      2. The Local Time Converter Application
    3. Introspection and Reflection
      1. The Class Class
      2. The Method Class
      3. The Class Browser Application
      4. The Introspection Test Application
      5. The Class Browser
      6. Disadvantages
    4. What’s Next?
    5. Summary
  33. Index
O'Reilly logo

image

CHAPTER

9

Java I/O

imageo far we have focused on Java language syntax, classes, interfaces, arrays, and other language-related stuff. Most of the programs discussed in earlier chapters used classes from the java.lang package, which were implicitly imported in the code. In a few programs, we used the classes from other packages such as java.util, java.awt, java.net, and java.io. Now that the language syntax is mostly covered, we will discuss the classes from these and other packages provided in Java libraries. One such important set of classes comes in ...

The best content for your career. Discover unlimited learning on demand for around $1/day.