Spec-Zone .ru
спецификации, руководства, описания, API
|
|
Java™ Platform Standard Ed. 7 DRAFT ea-b118 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.nio.file.Files
public final class Files extends Object
This class consists exclusively of static methods that operate on files or directories.
Modifier and Type | Method and Description |
---|---|
static void |
createDirectories(Path dir,
FileAttribute<?>... attrs)
Creates a directory by creating all nonexistent parent directories first. |
static String |
probeContentType(FileRef file)
Probes the content type of a file. |
static void |
walkFileTree(Path start,
FileVisitor<? super Path> visitor)
Walks a file tree. |
static void |
walkFileTree(Path start,
Set<FileVisitOption> options,
int maxDepth,
FileVisitor<? super Path> visitor)
Walks a file tree. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static String probeContentType(FileRef file) throws IOException
This method uses the installed FileTypeDetector
implementations
to probe the given file to determine its content type. Each file type
detector's probeContentType
is
invoked, in turn, to probe the file type. If the file is recognized then
the content type is returned. If the file is not recognized by any of the
installed file type detectors then a system-default file type detector is
invoked to guess the content type.
A given invocation of the Java virtual machine maintains a system-wide
list of file type detectors. Installed file type detectors are loaded
using the service-provider loading facility defined by the ServiceLoader
class. Installed file type detectors are loaded using the system class
loader. If the system class loader cannot be found then the extension class
loader is used; If the extension class loader cannot be found then the
bootstrap class loader is used. File type detectors are typically installed
by placing them in a JAR file on the application class path or in the
extension directory, the JAR file contains a provider-configuration file
named java.nio.file.spi.FileTypeDetector
in the resource directory
META-INF/services
, and the file lists one or more fully-qualified
names of concrete subclass of FileTypeDetector
that have a zero
argument constructor. If the process of locating or instantiating the
installed file type detectors fails then an unspecified error is thrown.
The ordering that installed providers are located is implementation
specific.
The return value of this method is the string form of the value of a
Multipurpose Internet Mail Extension (MIME) content type as
defined by
file
- The file referencenull
if the content
type cannot be determinedIOException
- If an I/O error occursSecurityException
- If a security manager is installed and it denies an unspecified
permission required by a file type detector implementation.public static void walkFileTree(Path start, Set<FileVisitOption> options, int maxDepth, FileVisitor<? super Path> visitor) throws IOException
This method walks a file tree rooted at a given starting file. The
file tree traversal is depth-first with the given FileVisitor
invoked for each file encountered. File tree traversal
completes when all accessible files in the tree have been visited, or a
visit method returns a result of TERMINATE
. Where a visit method terminates due an IOException
,
an uncaught error, or runtime exception, then the traversal is terminated
and the error or exception is propagated to the caller of this method.
For each file encountered this method attempts to gets its BasicFileAttributes
. If the file is not a
directory then the visitFile
method is
invoked with the file attributes. If the file attributes cannot be read,
due to an I/O exception, then the visitFileFailed
method is invoked with the I/O exception.
Where the file is a directory, and the directory could not be opened,
then the visitFileFailed
method is invoked with the I/O exception,
after which, the file tree walk continues, by default, at the next
sibling of the directory.
Where the directory is opened successfully, then the entries in the
directory, and their descendants are visited. When all entries
have been visited, or an I/O error occurs during iteration of the
directory, then the directory is closed and the visitor's postVisitDirectory
method is invoked.
The file tree walk then continues, by default, at the next sibling
of the directory.
By default, symbolic links are not automatically followed by this
method. If the options
parameter contains the FOLLOW_LINKS
option then symbolic links are
followed. When following links, and the attributes of the target cannot
be read, then this method attempts to get the BasicFileAttributes
of the link. If they can be read then the visitFile
method is
invoked with the attributes of the link (otherwise the visitFileFailed
method is invoked as specified above).
If the options
parameter contains the FOLLOW_LINKS
option then this method keeps
track of directories visited so that cycles can be detected. A cycle
arises when there is an entry in a directory that is an ancestor of the
directory. Cycle detection is done by recording the file-key
of directories,
or if file keys are not available, by invoking the isSameFile
method to test if a directory is the same file as an
ancestor. When a cycle is detected it is treated as an I/O error, and the
visitFileFailed
method is invoked with
an instance of FileSystemLoopException
.
The maxDepth
parameter is the maximum number of levels of
directories to visit. A value of 0
means that only the starting
file is visited, unless denied by the security manager. A value of
MAX_VALUE
may be used to indicate that all
levels should be visited. The visitFile
method is invoked for all
files, including directories, encountered at maxDepth
, unless the
basic file attributes cannot be read, in which case the visitFileFailed
method is invoked.
If a visitor returns a result of null
then NullPointerException
is thrown.
When a security manager is installed and it denies access to a file (or directory), then it is ignored and the visitor is not invoked for that file (or directory).
start
- The starting fileoptions
- Options to configure the traversalmaxDepth
- The maximum number of directory levels to visitvisitor
- The file visitor to invoke for each fileIllegalArgumentException
- If the maxDepth
parameter is negativeSecurityException
- If the security manager denies access to the starting file.
In the case of the default provider, the checkRead
method is invoked
to check read access to the directory.IOException
- If an I/O error is thrown by a visitor methodpublic static void walkFileTree(Path start, FileVisitor<? super Path> visitor) throws IOException
This method works as if invoking it were equivalent to evaluating the expression:
walkFileTree(start, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, visitor)
start
- The starting filevisitor
- The file visitor to invoke for each fileSecurityException
- If the security manager denies access to the starting file.
In the case of the default provider, the checkRead
method is invoked
to check read access to the directory.IOException
- If an I/O error is thrown by a visitor methodpublic static void createDirectories(Path dir, FileAttribute<?>... attrs) throws IOException
The attrs
parameter is an optional array of file-attributes
to set atomically when creating the nonexistent
directories. Each file attribute is identified by its name
. If more than one attribute of the same name is
included in the array then all but the last occurrence is ignored.
If this method fails, then it may do so after creating some, but not all, of the parent directories.
dir
- the directory to createattrs
- an optional list of file attributes to set atomically when
creating the directoryUnsupportedOperationException
- if the array contains an attribute that cannot be set atomically
when creating the directoryFileAlreadyExistsException
- if dir
exists but is not a directory (optional specific
exception)IOException
- if an I/O error occursSecurityException
- in the case of the default provider, and a security manager is
installed, the checkWrite
method is invoked prior to attempting to create a directory and
its checkRead
is
invoked for each parent directory that is checked. If dir
is not an absolute path then its toAbsolutePath
may need to be invoked to get its absolute path.
This may invoke the security manager's checkPropertyAccess
method to check access to the system property user.dir
|
Java™ Platform Standard Ed. 7 DRAFT ea-b118 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright © 1993, 2010, Oracle Corporation. All rights reserved.