|
NIO2 API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.classpath.icedtea.java.nio.file.Files
public final class Files
Utility methods for files and directories.
Method Summary | |
---|---|
static java.lang.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,
java.util.Set<FileVisitOption> options,
int maxDepth,
FileVisitor<? super Path> visitor)
Walks a file tree. |
static void |
withDirectory(Path dir,
DirectoryStream.Filter<? super Path> filter,
FileAction<? super Path> action)
Invokes a FileAction for each entry in a directory accepted
by a given filter . |
static void |
withDirectory(Path dir,
FileAction<? super Path> action)
Invokes a FileAction for all entries in a directory. |
static void |
withDirectory(Path dir,
java.lang.String glob,
FileAction<? super Path> action)
Invokes a FileAction for each entry in a directory with a
file name that matches a given pattern. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static java.lang.String probeContentType(FileRef file) throws java.io.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 RFC 2045: Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies. The string is guaranteed to be parsable according to the grammar in the RFC.
file
- The file reference
null
if the content
type cannot be determined
java.io.IOException
- If an I/O error occurs
java.lang.SecurityException
- If a security manager is installed and it denies an unspecified
permission required by a file type detector implementation.DirectoryStreamFilters.newContentTypeFilter(java.lang.String)
public static void withDirectory(Path dir, DirectoryStream.Filter<? super Path> filter, FileAction<? super Path> action) throws java.io.IOException
FileAction
for each entry in a directory accepted
by a given filter
.
This method opens the given directory and invokes the file action's
invoke
method for each entry accepted by the
filter. When iteration is completed then the directory is closed. If the
close
method throws an IOException
then it is silently ignored.
If the FileAction
's invoke
method terminates due
to an uncaught IOException
, Error
or RuntimeException
then the exception is propagated by this method after closing the
directory.
dir
- The directoryfilter
- The filter, or null
to accept all entriesaction
- The FileAction
to invoke for each accepted entry
NotDirectoryException
- If the dir
parameter is not a directory (optional
specific exception)
java.io.IOException
- If an I/O error occurs or the invoke
method terminates
due to an uncaught IOException
java.lang.SecurityException
- In the case of the default provider, the checkRead
method is invoked
to check read access to the directory.public static void withDirectory(Path dir, java.lang.String glob, FileAction<? super Path> action) throws java.io.IOException
FileAction
for each entry in a directory with a
file name that matches a given pattern.
This method opens the given directory and invokes the file action's
invoke
method for each entry that matches the
given pattern. When iteration is completed then the directory is closed.
If the close
method throws an IOException
then it is silently ignored.
If the FileAction
's invoke
method terminates due
to an uncaught IOException
, Error
or RuntimeException
then the exception is propagated by this method after closing the
directory.
The globbing pattern language supported by this method is as
specified by the getNameMatcher
method.
dir
- The directoryglob
- The globbing patternaction
- The FileAction
to invoke for each entry
NotDirectoryException
- If the dir
parameter is not a directory (optional
specific exception)
java.io.IOException
- If an I/O error occurs or the invoke
method terminates
due to an uncaught IOException
java.lang.SecurityException
- In the case of the default provider, the checkRead
method is invoked
to check read access to the directory.public static void withDirectory(Path dir, FileAction<? super Path> action) throws java.io.IOException
FileAction
for all entries in a directory.
This method works as if invoking it were equivalent to evaluating the expression:
withDirectory(dir, "*", action)
dir
- The directoryaction
- The FileAction
to invoke for each entry
NotDirectoryException
- If the dir
parameter is not a directory (optional
specific exception)
java.io.IOException
- If an I/O error occurs or the invoke
method terminates
due to an uncaught IOException
java.lang.SecurityException
- In the case of the default provider, the checkRead
method is invoked
to check read access to the directory.public static void walkFileTree(Path start, java.util.Set<FileVisitOption> options, int maxDepth, FileVisitor<? super Path> visitor)
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, a
visitor returns a result of TERMINATE
,
or the visitor terminates due to an uncaught Error
or RuntimeException
.
For each file encountered this method attempts to gets its java.nio.file.attribute.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, this method attempts to open it by
invoking its newDirectoryStream
method.
Where the directory could not be opened, due to an IOException
,
then the preVisitDirectoryFailed
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 DETECT_CYCLES
or FOLLOW_LINKS
options 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 the visitFile
is invoked with the attributes of the directory. The isDirectory
method may be used to test if the file is a directory and that a cycle is
detected. The preVisitDirectory
and postVisitDirectory
methods are not invoked.
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.
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 file
java.lang.IllegalArgumentException
- If the maxDepth
parameter is negative
java.lang.SecurityException
- 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.public static void walkFileTree(Path start, FileVisitor<? super Path> visitor)
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 file
java.lang.SecurityException
- 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.
|
NIO2 API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright 2010 Sun Microsystems, Inc. All rights reserved. Use is subject to the terms of the GNU General Public License.