JDKTM 7 Documentation




Java™ Platform
Standard Ed. 7

DRAFT ea-b59

java.nio.file.spi
Class AbstractPath

java.lang.Object
  extended by java.nio.file.Path
      extended by java.nio.file.spi.AbstractPath
All Implemented Interfaces:
Comparable<Path>, Iterable<Path>, FileRef, Watchable

public abstract class AbstractPath
extends Path

Base implementation class for a Path.

This class is intended to be extended by provider implementors. It implements, or provides default implementations for several of the methods defined by the Path class. It implements the copyTo and moveTo methods for the case that the source and target are not associated with the same provider.

Since:
1.7

Constructor Summary
Modifier Constructor and Description
protected AbstractPath()
          Initializes a new instance of this class.
 
Method Summary
Modifier and Type Method and Description
 Path copyTo(Path target, CopyOption... options)
          Copy the file located by this path to a target location.
 Path createFile(FileAttribute<?>... attrs)
          Creates a new and empty file, failing if the file already exists.
 void delete()
          Deletes the file referenced by this object.
 boolean exists()
          Tests whether the file located by this path exists.
protected abstract  void implCopyTo(Path target, CopyOption... options)
          Copy the file located by this path to a target location.
protected abstract  void implMoveTo(Path target, CopyOption... options)
          Move the file located by this path to a target location.
 Path moveTo(Path target, CopyOption... options)
          Move or rename the file located by this path to a target location.
 SeekableByteChannel newByteChannel(OpenOption... options)
          Opens or creates a file, returning a seekable byte channel to access the file.
 DirectoryStream<Path> newDirectoryStream()
          Opens the directory referenced by this object, returning a DirectoryStream to iterate over all entries in the directory.
 DirectoryStream<Path> newDirectoryStream(String glob)
          Opens the directory referenced by this object, returning a DirectoryStream to iterate over the entries in the directory.
 InputStream newInputStream()
          Opens the file located by this path for reading, returning an input stream to read bytes from the file.
 OutputStream newOutputStream(OpenOption... options)
          Opens or creates the file located by this path for writing, returning an output stream to write bytes to the file.
 OutputStream newOutputStream(Set<? extends OpenOption> options, FileAttribute<?>... attrs)
          Opens or creates the file located by this path for writing, returning an output stream to write bytes to the file.
 boolean notExists()
          Tests whether the file located by this path does not exist.
 WatchKey register(WatchService watcher, WatchEvent.Kind<?>... events)
          Registers the file located by this path with a watch service.
 
Methods inherited from class java.nio.file.Path
compareTo, createDirectory, createLink, createSymbolicLink, delete, endsWith, equals, getFileSystem, getName, getName, getNameCount, getParent, getRoot, hashCode, isAbsolute, isHidden, iterator, newByteChannel, newDirectoryStream, normalize, readSymbolicLink, register, relativize, resolve, resolve, startsWith, subpath, toAbsolutePath, toRealPath, toString, toUri
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.nio.file.FileRef
checkAccess, getFileAttributeView, getFileAttributeView, getFileStore, isSameFile
 

Constructor Detail

AbstractPath

protected AbstractPath()
Initializes a new instance of this class.

Method Detail

delete

public void delete()
            throws IOException
Deletes the file referenced by this object.

This method invokes the delete(boolean) method with a parameter of true. It may be overridden where required.

Throws:
NoSuchFileException - If the file does not exist (optional specific exception)
DirectoryNotEmptyException - If the file is a directory and could not otherwise be deleted because the directory is not empty (optional specific exception)
IOException - If an I/O error occurs
SecurityException - In the case of the default provider, and a security manager is installed, the SecurityManager.checkDelete(String) method is invoked to check delete access to the file

createFile

public Path createFile(FileAttribute<?>... attrs)
                throws IOException
Creates a new and empty file, failing if the file already exists.

This method invokes the newByteChannel(Set,FileAttribute...) method to create the file. It may be overridden where required.

Specified by:
createFile in class Path
Parameters:
attrs - an optional list of file attributes to set atomically when creating the file
Returns:
this path
Throws:
IllegalArgumentException
FileAlreadyExistsException - if a file of that name already exists (optional specific exception)
IOException - if an I/O error occurs
SecurityException - In the case of the default provider, and a security manager is installed, the checkWrite method is invoked to check write access to the new file.

newByteChannel

public SeekableByteChannel newByteChannel(OpenOption... options)
                                   throws IOException
Opens or creates a file, returning a seekable byte channel to access the file.

This method invokes the newByteChannel(Set,FileAttribute...) method to open or create the file. It may be overridden where required.

Specified by:
newByteChannel in interface FileRef
Specified by:
newByteChannel in class Path
Parameters:
options - options specifying how the file is opened
Returns:
a new seekable byte channel
Throws:
IllegalArgumentException - if the set contains an invalid combination of options
FileAlreadyExistsException - if a file of that name already exists and the CREATE_NEW option is specified (optional specific exception)
IOException - If an I/O error occurs
SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the path if the file is opened for reading. The checkWrite method is invoked to check write access to the path if the file is opened for writing.

newInputStream

public InputStream newInputStream()
                           throws IOException
Opens the file located by this path for reading, returning an input stream to read bytes from the file.

This method returns an InputStream that is constructed by invoking the Channels.newInputStream method. It may be overridden where a more efficient implementation is available.

Specified by:
newInputStream in class Path
Returns:
an input stream to read bytes from the file
Throws:
IOException - if an I/O error occurs
SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the file.

newOutputStream

public OutputStream newOutputStream(OpenOption... options)
                             throws IOException
Opens or creates the file located by this path for writing, returning an output stream to write bytes to the file.

This method returns an OutputStream that is constructed by invoking the Channels.newOutputStream method. It may be overridden where a more efficient implementation is available.

Specified by:
newOutputStream in class Path
Parameters:
options - options specifying how the file is opened
Returns:
a new seekable byte channel
Throws:
IllegalArgumentException - if options contains an invalid combination of options
IOException - if an I/O error occurs
SecurityException - In the case of the default provider, and a security manager is installed, the checkWrite method is invoked to check write access to the file.

newOutputStream

public OutputStream newOutputStream(Set<? extends OpenOption> options,
                                    FileAttribute<?>... attrs)
                             throws IOException
Opens or creates the file located by this path for writing, returning an output stream to write bytes to the file.

This method returns an OutputStream that is constructed by invoking the Channels.newOutputStream method. It may be overridden where a more efficient implementation is available.

Specified by:
newOutputStream in class Path
Parameters:
options - options specifying how the file is opened
attrs - an optional list of file attributes to set atomically when creating the file
Returns:
a new output stream
Throws:
IllegalArgumentException - if the set contains an invalid combination of options
IOException - if an I/O error occurs
SecurityException - In the case of the default provider, and a security manager is installed, the checkWrite method is invoked to check write access to the file.

newDirectoryStream

public DirectoryStream<Path> newDirectoryStream()
                                         throws IOException
Opens the directory referenced by this object, returning a DirectoryStream to iterate over all entries in the directory.

This method invokes the newDirectoryStream(Filter) method with a filter that accept all entries. It may be overridden where required.

Specified by:
newDirectoryStream in class Path
Returns:
a new and open DirectoryStream object
Throws:
NotDirectoryException - if the file could not otherwise be opened because it is not a directory (optional specific exception)
IOException - if an I/O error occurs
SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the directory.

newDirectoryStream

public DirectoryStream<Path> newDirectoryStream(String glob)
                                         throws IOException
Opens the directory referenced by this object, returning a DirectoryStream to iterate over the entries in the directory. The entries are filtered by matching the String representation of their file names against a given pattern.

This method constructs a PathMatcher by invoking the file system's getPathMatcher method. This method may be overridden where a more efficient implementation is available.

Specified by:
newDirectoryStream in class Path
Parameters:
glob - the glob pattern
Returns:
a new and open DirectoryStream object
Throws:
PatternSyntaxException - if the pattern is invalid
UnsupportedOperationException - if the pattern syntax is not known to the implementation
NotDirectoryException - if the file could not otherwise be opened because it is not a directory (optional specific exception)
IOException - if an I/O error occurs
SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the directory.

exists

public boolean exists()
Tests whether the file located by this path exists.

This method invokes the checkAccess method to check if the file exists. It may be overridden where a more efficient implementation is available.

Specified by:
exists in class Path
Returns:
true if the file exists; false if the file does not exist or its existence cannot be determined.
See Also:
Path.notExists()

notExists

public boolean notExists()
Tests whether the file located by this path does not exist.

This method invokes the checkAccess method to check if the file exists. It may be overridden where a more efficient implementation is available.

Specified by:
notExists in class Path
Returns:
true if the file does not exist; false if the file exists or its existence cannot be determined.

register

public WatchKey register(WatchService watcher,
                         WatchEvent.Kind<?>... events)
                  throws IOException
Registers the file located by this path with a watch service.

This method invokes the register(WatchService,WatchEvent.Kind[],WatchEvent.Modifier...) method to register the file. It may be overridden where required.

Specified by:
register in interface Watchable
Specified by:
register in class Path
Parameters:
watcher - The watch service to which this object is to be registered
events - The events for which this object should be registered
Returns:
A key representing the registration of this object with the given watch service
Throws:
NotDirectoryException - If the file is registered to watch the entries in a directory and the file is not a directory (optional specific exception)
IOException - If an I/O error occurs

implCopyTo

protected abstract void implCopyTo(Path target,
                                   CopyOption... options)
                            throws IOException
Copy the file located by this path to a target location.

This method is invoked by the copyTo method for the case that this Path and the target Path are associated with the same provider.

Parameters:
target - The target location
options - Options specifying how the copy should be done
Throws:
IllegalArgumentException - If an invalid option is specified
FileAlreadyExistsException - The target file exists and cannot be replaced because the REPLACE_EXISTING option is not specified, or the target file is a non-empty directory (optional specific exception)
IOException - If an I/O error occurs
SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the source file, the checkWrite is invoked to check write access to the target file. If a symbolic link is copied the security manager is invoked to check LinkPermission("symbolic").

implMoveTo

protected abstract void implMoveTo(Path target,
                                   CopyOption... options)
                            throws IOException
Move the file located by this path to a target location.

This method is invoked by the moveTo method for the case that this Path and the target Path are associated with the same provider.

Parameters:
target - The target location
options - Options specifying how the move should be done
Throws:
IllegalArgumentException - If an invalid option is specified
FileAlreadyExistsException - The target file exists and cannot be replaced because the REPLACE_EXISTING option is not specified, or the target file is a non-empty directory
AtomicMoveNotSupportedException - The options array contains the ATOMIC_MOVE option but the file cannot be moved as an atomic file system operation.
IOException - If an I/O error occurs
SecurityException - In the case of the default provider, and a security manager is installed, the checkWrite method is invoked to check write access to both the source and target file.

copyTo

public final Path copyTo(Path target,
                         CopyOption... options)
                  throws IOException
Copy the file located by this path to a target location.

If this path is associated with the same provider as the target then the implCopyTo method is invoked to copy the file. Otherwise, this method attempts to copy the file to the target location in a manner that may be less efficient than would be the case that target is associated with the same provider as this path.

Specified by:
copyTo in class Path
Parameters:
target - the target location
options - options specifying how the copy should be done
Returns:
the target
Throws:
IllegalArgumentException
FileAlreadyExistsException - if the target file exists and cannot be replaced because the REPLACE_EXISTING option is not specified, or the target file is a non-empty directory (optional specific exception)
IOException - if an I/O error occurs
SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the source file, the checkWrite is invoked to check write access to the target file. If a symbolic link is copied the security manager is invoked to check LinkPermission("symbolic").

moveTo

public final Path moveTo(Path target,
                         CopyOption... options)
                  throws IOException
Move or rename the file located by this path to a target location.

If this path is associated with the same provider as the target then the implMoveTo method is invoked to move the file. Otherwise, this method attempts to copy the file to the target location and delete the source file. This implementation may be less efficient than would be the case that target is associated with the same provider as this path.

Specified by:
moveTo in class Path
Parameters:
target - the target location
options - options specifying how the move should be done
Returns:
the target
Throws:
IllegalArgumentException
FileAlreadyExistsException - if the target file exists and cannot be replaced because the REPLACE_EXISTING option is not specified, or the target file is a non-empty directory
IOException - if an I/O error occurs
SecurityException - In the case of the default provider, and a security manager is installed, the checkWrite method is invoked to check write access to both the source and target file.

Java™ Platform
Standard Ed. 7

DRAFT ea-b59