You are previewing Java 7 New Features Cookbook.
O'Reilly logo
Java 7 New Features Cookbook

Book Description

Get up to speed with Java 7 the easy, enjoyable way. With over 100 recipes, this Cookbook lets you explore and try out new features at your own pace. From language improvements to GUI creation, it’s all here.

  • Comprehensive coverage of the new features of Java 7 organized around easy-to-follow recipes

  • Covers exciting features such as the try-with-resources block, the monitoring of directory events, asynchronous IO and new GUI enhancements, and more

  • A learn-by-example based approach that focuses on key concepts to provide the foundation to solve real world problems

  • In Detail

    Java 7 is a major update that includes a lot of exciting new language improvements such as support for type inference and improved exception handling. Other new features include the ability to work with symbolic links, a greatly simplified directory traversal technique, and the monitoring of file creation and deletion. Improvements in event handling, security, and concurrent processing have also been added

    Java 7 New Features Cookbook is your go-to guide to learn about all the new exciting features Java 7 has to offer with a very practical recipe-based approach.

    The book starts with coverage of the new language improvements. Subsequent chapters address the new features of Java 7 while incorporating these new language improvements when possible.

    The new NIO techniques provide enhanced capabilities which are complemented by the new try-with-resources block and enhanced generic support. The new JLayer decorator and improved window methods enhance the developer’s ability to create GUI applications.

    The Java 7 New Features Cookbook provides a comprehensive coverage of the exciting features in Java 7.

    Table of Contents

    1. Java 7 New Features Cookbook
    2. Credits
    3. About the Authors
    4. Acknowledgement
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
        3. Instant Updates on New Packt Books
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
      7. Downloading the example code
        1. Errata
        2. Piracy
        3. Questions
    8. 1. Java Language Improvements
      1. Introduction
      2. Using string literals in switch statements
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      3. Using underscores in literals to improve code readability
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Simple underscore usage mistakes
          2. Using underscores with hexadecimal literals
          3. Using underscores with binary literals
      4. Using the try-with-resources block to improve exception handling code
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Understanding suppressed exceptions
          2. Structuring issues when using the try-with-resources technique
        5. See also
      5. Creating a resource that can be used with the try-with-resources technique
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Catching multiple exception types to improve type checking
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. The use of a common exception base class and the ReflectiveOperationException
          2. Using the AssertionError class in Java 7
        5. See also
      7. Rethrowing exceptions in Java 7
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      8. Using the diamond operator for constructor type inference
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using the diamond operator when the type is not obvious
          2. Suppressing unchecked warnings
          3. Understanding erasure
      9. Using the @SafeVarargs annotation
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. The use of @SafeVarargs annotation in Java core libraries
          2. An example of heap pollution
        5. See also
    9. 2. Locating Files and Directories Using Paths
      1. Introduction
      2. Creating a Path object
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using the Paths class' get method
          2. The meaning of the parent path
        5. See also
      3. Interoperability between java.io.File and java.nio.file.Files
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      4. Converting a relative path into an absolute path
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Removing redundancies by normalizing a path
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Combining paths using path resolution
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using a String argument with the resolve method
          2. Improper use of the resolve method
          3. Using the resolveSibling
        5. See also
      7. Creating a path between two locations
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Both paths are equal
          2. One path contains a root
          3. Both paths contain a root
        5. See also
      8. Converting between path types
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      9. Determining whether two paths are equivalent
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      10. Managing symbolic links
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
    10. 3. Obtaining File and Directory Information
      1. Introduction
      2. Determining the file content type
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      3. Obtaining a single attribute at a time using the getAttribute method
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      4. Obtaining a map of file attributes
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      5. Getting file and directory information
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      6. Determining operating system support for attribute views
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using the supportsFileAttributeView method with a class argument
          2. Using the supportsFileAttributeView method with a String argument
      7. Maintaining basic file attributes using the BasicFileAttributeView
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      8. Maintaining POSIX file attributes using the PosixFileAttributeView
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Maintaining FAT table attributes using the DosFileAttributeView
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      10. Maintaining file ownership attributes using the FileOwnerAttributeView
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      11. Maintaining a file's ACL using the AclFileAttributeView
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      12. Maintaining user-defined file attributes using the UserDefinedFileAttributeView
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
    11. 4. Managing Files and Directories
      1. Introduction
      2. Creating files and directories
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using the createDirectories method to create a hierarchy of directories
        5. See also
      3. Controlling how a file is copied
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Copying a symbolic link file
          2. Copying a directory
          3. Copying an input stream to a file
          4. Copying a file to an output stream
        5. See also
      4. Managing temporary files and directories
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      5. Setting time-related attributes of a file or directory
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Understanding the FileTime class
          2. Using the Files class' setLastModifiedTime to maintain the last modified time
          3. Using the Files class' setAttribute method to set individual attributes
        5. See also
      6. Managing file ownership
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using the Files class' setOwner method
      7. Managing ACL file permissions
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      8. Managing POSIX attributes
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Removing a file permission
          2. Modifying the POSIX ownership of a file
          3. Using the Files class' set/get POSIX file permission methods
          4. Using the Files class' setAttribute method
          5. Using the PosixFilePermissions class to create PosixFilePermissions
      9. Moving a file and a directory
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Trivial uses of the move method
          2. The meaning of the StandardCopyOption enumeration values
          3. Using the resolveSibling method with the move method to affect a rename operation
          4. Moving a directory
      10. Deleting files or directories
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      11. Managing symbolic links
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Creating a hard link
          2. Creating a symbolic link to a directory
          3. Determining the target of a link file
    12. 5. Managing Filesystems
      1. Introduction
      2. Getting FileStore information
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      3. Getting Filesystem information
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      4. Using the SimpleFileVisitor class to traverse filesystems
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Deleting a directory using the SimpleFileVisitor class
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      6. Copying a directory using the SimpleFileVisitor class
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      7. Processing the contents of a directory by using the DirectoryStream interface
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Filtering a directory using globbing
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using the PathMatcher interface to filter a directory
        5. See also
      9. Writing your own directory filter
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      10. Monitoring file events using WatchEvents
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      11. Understanding the ZIP filesystem provider
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
    13. 6. Stream IO in Java 7
      1. Introduction
      2. Managing simple files
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Writing to a simple file
          2. Reading all of the lines of a file returned as a list
        5. See also
      3. Using buffered IO for files
        1. Getting ready
        2. How to do it...
          1. How it works...
          2. There's more...
            1. Writing to a file using the BufferedWriter class
            2. Un-buffered IO support in the Files class
          3. See also
        3. Random access IO using the SeekableByteChannel
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
              1. Processing the contents of the entire file
              2. Writing to a file using the SeekableByteChannel interface
              3. Query the position
            3. See also
          3. Managing asynchronous communication using the AsynchronousServerSocketChannel class
            1. Getting ready
            2. How to do it...
            3. How it works...
            4. There's more...
              1. Using the Future object in a server
              2. Understanding the AsynchronousServerSocketChannel class options
            5. See also
          4. Writing to a file using the AsynchronousFileChannel class
            1. Getting ready
            2. How to do it...
            3. How it works...
            4. There's more...
            5. See also
          5. Reading from a file using the AsynchronousFileChannel class
            1. Getting ready
            2. How to do it...
            3. How it works...
            4. There's more...
            5. See also
          6. Using the SecureDirectoryStream class
            1. Getting ready
            2. How to do it...
              1. How it works...
              2. There's more...
    14. 7. Graphical User Interface Improvements
      1. Introduction
      2. Mixing heavyweight and lightweight components
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      3. Managing window types
        1. Getting ready
        2. How to do it...
        3. How it works...
      4. Managing the opacity of a window
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Creating a varying gradient translucent window
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      6. Managing the shape of a window
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      7. Using the new border types in Java 7
        1. Getting ready
        2. How to do it...
        3. How it works...
      8. Handling multiple file selection in the FileDialog class
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      9. Controlling the print dialog box type
        1. Getting ready
        2. How to do it...
        3. How it works...
      10. Using the new JLayer decorator for a password field
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
    15. 8. Handling Events
      1. Introduction
      2. Managing extra mouse buttons and high resolution mouse wheels
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      3. Controlling a focus when displaying a window
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      4. Using secondary loops to mimic modal dialog boxes
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Handling spurious thread wakeups
        1. Getting ready
        2. How to do it...
        3. How it works...
      6. Handling applet initialization status with event handlers
        1. Getting ready
        2. How to do it...
        3. How it works...
    16. 9. Database, Security, and System Enhancements
      1. Introduction
      2. Using the RowSetFactory class
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      3. Java 7 database enhancements
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Retrieving pseudo-columns
          2. Controlling the type value of the OUT parameter
          3. Other database enhancements
      4. Using the ExtendedSSLSession interface
        1. Getting ready
        2. How to do it...
        3. How it works...
      5. Using the platform MXBeans for JVM or system process load monitoring
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using the getPlatformMXBeans method
          2. Obtaining the platform's management interfaces
      6. Redirecting input and output from operating system's processes
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using the ProcessBuilder.Redirect class
          2. Using the inheritIO method to inherit the default IO locations
      7. Embedding a JNLP file in an HTML page
        1. Getting ready
        2. How to do it...
        3. How it works...
    17. 10. Concurrent Processing
      1. Introduction
      2. Using join/fork framework in Java
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Using the reusable synchronization barrier Phaser
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using a phaser to repeat a series of tasks
        5. See also
      4. Using the new ConcurrentLinkedDeque safely with multiple threads
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Problems with asynchronous concurrent threads
          2. Adding elements to the deque
          3. Retrieving elements from the deque
          4. Accessing a specific element of the deque
      5. Using the new LinkedTransferQueue class
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Problems with asynchronous concurrent threads
          2. Adding elements to the queue
          3. Retrieving elements from the deque
      6. Supporting multiple threads using the ThreadLocalRandom class
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    18. 11. Odds and Ends
      1. Introduction
        1. Unicode 6.0
        2. Primitive types and the compare method
        3. Global logger
        4. JavaDocs improvements
        5. JVM performance enhancements
      2. Handling weeks in Java 7
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      3. Using the Currency class in Java 7
        1. Getting ready
        2. How to do it...
        3. How it works...
      4. Using the NumericShaper.Range enumeration to support the display of digits
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      5. JavaBean enhancements in Java 7
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      6. Handling locales and the Locale.Builder class in Java 7
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using the Locale.Category enumeration to display information using two different locales
      7. Handling null references
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Additional Objects class methods
          2. Using empty iterators to avoid null pointer exceptions
      8. Using the new BitSet methods in Java 7
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...