Pro Java 7 NIO.2 [Leonard 2011-12-06].pdf

(4250 KB) Pobierz
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
Contents at a Glance
Contents at a Glance .......................................................................................................iii
Contents .......................................................................................................................... iv
About the Author ........................................................................................................... xiii
About the Technical Reviewer ....................................................................................... xiv
Acknowledgments .......................................................................................................... xv
Preface .......................................................................................................................... xvi
Chapter 1: Working with the Path Class ...........................................................................1
Chapter 2: Metadata File Attributes ...............................................................................11
Chapter 3: Manage Symbolic and Hard Links.................................................................35
Chapter 4: Files and Directories .....................................................................................43
Chapter 5: Recursive Operations: Walks ........................................................................77
Chapter 6: Watch Service API .......................................................................................111
Chapter 7: Random Access Files ..................................................................................135
Chapter 8: The Sockets APIs ........................................................................................169
Chapter 9: The Asynchronous Channel API ..................................................................215
Chapter 10: Important Things to Remember ................................................................263
Index ............................................................................................................................273
iii
CHAPTER 1
■■■
Working with the Path Class
The recommended entry point to start exploring the NIO.2 API, also known as “JSR 203: More New I/O
APIs for the Java Platform” (NIO.2), is the new abstract class
java.nio.file.Path.
This class is a
milestone of NIO.2, and every application that involves I/O operations will exploit the powerful facilities
of this class. Practically, it is the most commonly used class of NIO.2, since many I/O operations are
based on a
Path
resource.
The
Path
class supports two types of operations:
syntactic operations
(almost any operation that
involves manipulating paths without accessing the file system; these are logical manipulations done in
memory) and
operations over files
referenced by paths. This chapter covers the first type of operations
and introduces you to the
Path
API. In Chapter 4, I focus on exploring the second type of operations. The
concepts presented in this chapter will be very useful in the rest of the book.
Introducing the Path Class
A path resides in a
file system,
which “stores and organizes files on some form of media, generally one or
more hard drives, in such a way that they can be easily retrieved.”
1
The file system can be accessed
through the
java.nio.file.FileSystems
final class, which is used to get an instance of the
java.nio.file.FileSystem
we want to work on.
FileSystems
contains the following two important
methods, as well as a set of
newFileSystem()
methods, for constructing new file systems:
getDefault():
This is a static method that returns the default
FileSystem
to the
JVM—commonly the operating system default file system.
1
Oracle,
The Java Tutorials,
“What Is a Path? (And Other File System Facts),”
http://download.oracle.com/javase/tutorial/essential/io/path.html.
1
CHAPTER 1
WORKING WITH THE PATH CLASS
getFileSystem(URI uri):
This is a static method that returns a file system from
the set of available file system providers that match the given URI schema. The
Path
class manipulates a file in any file system (FileSystem) that can use any
storage place (java.nio.file.FileStore; this class represents the underlying
storage). By default (and commonly), the
Path
refers to files in the default file
system (the file system of the computer), but NIO.2 is totally modular—an
implementation of
FileSystem
for data in memory, on the network, or on a virtual
file system is perfectly agreeable to NIO.2. NIO.2 provides us with all file system
functionalities that we may need to perform over a file, a directory, or a link.
The
Path
class is an upgraded version of the well-known
java.io.File
class, but the
File
class has
kept a few specific operations, so it is not deprecated and cannot be considered obsolete. Moreover,
starting with Java 7, both classes are available, which means programmers can mix their powers to
obtain the best of I/O APIs. Java 7 provides a simple API for conversion between them. Remember the
days when you had to do the following?
import java.io.File;
File file = new File("index.html");
Well, those days are gone, because with Java 7 you can do this:
import java.nio.file.Path;
import java.nio.file.Paths;
Path path = Paths.get("index.html");
At a closer look, a
Path
is a programmatic representation of a path in the file system. The path string
contains the file name, the directory list, and the OS-dependent file delimiter (e.g., backslash “\” on
Microsoft Windows and forward slash “/” on Solaris and Linux), which means that a
Path
is not system
independent since it is based on a system-dependent string path. Because
Path
is basically a string, the
referenced resource might not exist.
Defining a Path
Once you identify the file system and the location of a file or directory, you can create a
Path
object for it.
Absolute paths, relative paths, paths defined with the notation “.” (indicates the current directory) or
“..” (indicates the parent directory), and paths containing only a file/directory name are covered by the
Path
class. The simplest solution for defining a
Path
is to call one of the
get()
methods of the
Paths
helper class. The following subsections present several different ways to define a path to the same file
(on Windows)—C:\rafaelnadal\tournaments\2009\BNP.txt.
Define an Absolute Path
An absolute path (also known as a full path or file path) is a path that contains the root directory and all
other subdirectories that contain a file or folder. Defining an absolute path in NIO.2 is a one-line-of-code
task, as you can see in the following example, which points to the file named
BNP.txt
in the
C:\rafaelnadal\tournaments\2009
directory (the file may not exist for testing this code):
Path path = Paths.get("C:/rafaelnadal/tournaments/2009/BNP.txt");
2
Zgłoś jeśli naruszono regulamin