The NIO.2 API introduced in the Java SE 7 release provides the
ability to develop a custom file system provider that can be used
to manage file system objects. A file system is
essentially a container with organized, homogenous elements
referred to as file system objects. A file system provides access
to file system objects. A file system object can be a file
store, file, or directory. A file store is a volume or
partition in which files are stored. For example, in a native file
system such as on the Windows platform, commonly known drives like
c:
or d:
are file stores. On the Solaris
operating system, /
(root) and mounted directories are
considered file stores.
The java.nio.file.spi.FileSystemProvider
class allows you to develop a custom file system provider. A custom
file system provider is useful in the following situations:
java.nio.file.spi.FileSystemProvider
ClassA custom file system provider must implement the java.nio.file.spi.FileSystemProvider
class. A file system provider is identified by a URI scheme such as
file
, jar
, memory
,
cd
.
An implementation of the
java.nio.file.spi.FileSystemProvider
class is a
factory for instances of the java.nio.file.FileSystem
class. A file system's URI has a URI scheme that matches the URI
scheme of the file system provider that created it.
The
newFileSystem
method is used to create a file
system and the
getFileSystem
method is used to retrieve a
reference to an existing file system.
This section describes the high-level steps necessary to create
a custom file system provider using the
java.nio.file.spi.FileSystemProvider
API. The
ZipFileSystemProvider
class that is shipped in the
demo/nio/zipfs
of your JDK installation is an example
of a custom file system provider. See Resources for information about the zip file
system provider.
Implementing the custom file system provider class involves the following operations:
MyFileSystemProvider
, that extends the
java.nio.file.spi.FileSystemProvider
class.jar
for the file
system provider. The getScheme
method should return
the URI scheme of this provider.newFileSystem
method. The method
will create a new custom file system at the specified path and add
the file system to cache. This method should throw a
java.nio.file.FileSystemAlreadyExistsException
exception if a file system already exists at the specified
path.getFileSystem
method. This method
should search the cache and return a previously created instance of
a file system that corresponds to the given URI.newFileChannel
method or the
newAsynchronousFileChannel
method depending on the
requirements of your file system provider. This method should
return a FileChannel
object that allows a file to be
read or written in the file system.Implementing the custom file system class involves the following operations:
MyFileSystem
, that extends
the java.nio.file.FileSystem
class.