NIO2 API

org.classpath.icedtea.java.nio.file
Class Files

java.lang.Object
  extended by org.classpath.icedtea.java.nio.file.Files

public final class Files
extends Object

Utility methods for files and directories.

Since:
1.7

Method Summary
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.
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, 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

probeContentType

public static String probeContentType(FileRef file)
                               throws IOException
Probes the content type of a file.

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.

Parameters:
file - The file reference
Returns:
The content type of the file, or null if the content type cannot be determined
Throws:
IOException - If an I/O error occurs
SecurityException - If a security manager is installed and it denies an unspecified permission required by a file type detector implementation.
See Also:
DirectoryStreamFilters.newContentTypeFilter(java.lang.String)

withDirectory

public static void withDirectory(Path dir,
                                 DirectoryStream.Filter<? super Path> filter,
                                 FileAction<? super Path> action)
                          throws IOException
Invokes a 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.

Parameters:
dir - The directory
filter - The filter, or null to accept all entries
action - The FileAction to invoke for each accepted entry
Throws:
NotDirectoryException - If the dir parameter is not a directory (optional specific exception)
IOException - If an I/O error occurs or the invoke method terminates due to an uncaught IOException
SecurityException - In the case of the default provider, the checkRead method is invoked to check read access to the directory.

withDirectory

public static void withDirectory(Path dir,
                                 String glob,
                                 FileAction<? super Path> action)
                          throws IOException
Invokes a 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.

Parameters:
dir - The directory
glob - The globbing pattern
action - The FileAction to invoke for each entry
Throws:
NotDirectoryException - If the dir parameter is not a directory (optional specific exception)
IOException - If an I/O error occurs or the invoke method terminates due to an uncaught IOException
SecurityException - In the case of the default provider, the checkRead method is invoked to check read access to the directory.

withDirectory

public static void withDirectory(Path dir,
                                 FileAction<? super Path> action)
                          throws IOException
Invokes a FileAction for all entries in a directory.

This method works as if invoking it were equivalent to evaluating the expression:

 withDirectory(dir, "*", action)
 

Parameters:
dir - The directory
action - The FileAction to invoke for each entry
Throws:
NotDirectoryException - If the dir parameter is not a directory (optional specific exception)
IOException - If an I/O error occurs or the invoke method terminates due to an uncaught IOException
SecurityException - In the case of the default provider, the checkRead method is invoked to check read access to the directory.

walkFileTree

public static void walkFileTree(Path start,
                                Set<FileVisitOption> options,
                                int maxDepth,
                                FileVisitor<? super Path> visitor)
Walks a file tree.

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 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).

Parameters:
start - The starting file
options - Options to configure the traversal
maxDepth - The maximum number of directory levels to visit
visitor - The file visitor to invoke for each file
Throws:
IllegalArgumentException - If the maxDepth parameter is negative
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.

walkFileTree

public static void walkFileTree(Path start,
                                FileVisitor<? super Path> visitor)
Walks a file tree.

This method works as if invoking it were equivalent to evaluating the expression:

 walkFileTree(start, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, visitor)
 

Parameters:
start - The starting file
visitor - The file visitor to invoke for each file
Throws:
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

Copyright © 2007, 2011, Oracle and/or its affiliates. All rights reserved.