< prev index next >

src/java.base/share/classes/java/nio/file/Files.java

Print this page




 385      *          StandardOpenOption#CREATE_NEW CREATE_NEW} option is specified
 386      *          <i>(optional specific exception)</i>
 387      * @throws  IOException
 388      *          if an I/O error occurs
 389      * @throws  SecurityException
 390      *          In the case of the default provider, and a security manager is
 391      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
 392      *          method is invoked to check read access to the path if the file is
 393      *          opened for reading. The {@link SecurityManager#checkWrite(String)
 394      *          checkWrite} method is invoked to check write access to the path
 395      *          if the file is opened for writing. The {@link
 396      *          SecurityManager#checkDelete(String) checkDelete} method is
 397      *          invoked to check delete access if the file is opened with the
 398      *          {@code DELETE_ON_CLOSE} option.
 399      *
 400      * @see java.nio.channels.FileChannel#open(Path,OpenOption[])
 401      */
 402     public static SeekableByteChannel newByteChannel(Path path, OpenOption... options)
 403         throws IOException
 404     {
 405         Set<OpenOption> set = new HashSet<OpenOption>(options.length);
 406         Collections.addAll(set, options);
 407         return newByteChannel(path, set);
 408     }
 409 
 410     // -- Directories --
 411 
 412     private static class AcceptAllFilter
 413         implements DirectoryStream.Filter<Path>
 414     {
 415         private AcceptAllFilter() { }
 416 
 417         @Override
 418         public boolean accept(Path entry) { return true; }
 419 
 420         static final AcceptAllFilter FILTER = new AcceptAllFilter();
 421     }
 422 
 423     /**
 424      * Opens a directory, returning a {@link DirectoryStream} to iterate over
 425      * all entries in the directory. The elements returned by the directory


 499      * @throws  NotDirectoryException
 500      *          if the file could not otherwise be opened because it is not
 501      *          a directory <i>(optional specific exception)</i>
 502      * @throws  IOException
 503      *          if an I/O error occurs
 504      * @throws  SecurityException
 505      *          In the case of the default provider, and a security manager is
 506      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
 507      *          method is invoked to check read access to the directory.
 508      */
 509     public static DirectoryStream<Path> newDirectoryStream(Path dir, String glob)
 510         throws IOException
 511     {
 512         // avoid creating a matcher if all entries are required.
 513         if (glob.equals("*"))
 514             return newDirectoryStream(dir);
 515 
 516         // create a matcher and return a filter that uses it.
 517         FileSystem fs = dir.getFileSystem();
 518         final PathMatcher matcher = fs.getPathMatcher("glob:" + glob);
 519         DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
 520             @Override
 521             public boolean accept(Path entry)  {
 522                 return matcher.matches(entry.getFileName());
 523             }
 524         };
 525         return fs.provider().newDirectoryStream(dir, filter);
 526     }
 527 
 528     /**
 529      * Opens a directory, returning a {@link DirectoryStream} to iterate over
 530      * the entries in the directory. The elements returned by the directory
 531      * stream's {@link DirectoryStream#iterator iterator} are of type {@code
 532      * Path}, each one representing an entry in the directory. The {@code Path}
 533      * objects are obtained as if by {@link Path#resolve(Path) resolving} the
 534      * name of the directory entry against {@code dir}. The entries returned by
 535      * the iterator are filtered by the given {@link DirectoryStream.Filter
 536      * filter}.
 537      *
 538      * <p> When not using the try-with-resources construct, then directory
 539      * stream's {@code close} method should be invoked after iteration is


1524      *          if an I/O error occurs
1525      * @throws  SecurityException
1526      *          In the case of the default provider, and a security manager is
1527      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
1528      *          method is invoked to check read access to the file.
1529      */
1530     public static boolean isHidden(Path path) throws IOException {
1531         return provider(path).isHidden(path);
1532     }
1533 
1534     // lazy loading of default and installed file type detectors
1535     private static class FileTypeDetectors{
1536         static final FileTypeDetector defaultFileTypeDetector =
1537             createDefaultFileTypeDetector();
1538         static final List<FileTypeDetector> installedDetectors =
1539             loadInstalledDetectors();
1540 
1541         // creates the default file type detector
1542         private static FileTypeDetector createDefaultFileTypeDetector() {
1543             return AccessController
1544                 .doPrivileged(new PrivilegedAction<FileTypeDetector>() {
1545                     @Override public FileTypeDetector run() {
1546                         return sun.nio.fs.DefaultFileTypeDetector.create();
1547                 }});
1548         }
1549 
1550         // loads all installed file type detectors
1551         private static List<FileTypeDetector> loadInstalledDetectors() {
1552             return AccessController
1553                 .doPrivileged(new PrivilegedAction<List<FileTypeDetector>>() {
1554                     @Override public List<FileTypeDetector> run() {
1555                         List<FileTypeDetector> list = new ArrayList<>();
1556                         ServiceLoader<FileTypeDetector> loader = ServiceLoader
1557                             .load(FileTypeDetector.class, ClassLoader.getSystemClassLoader());
1558                         for (FileTypeDetector detector: loader) {
1559                             list.add(detector);
1560                         }
1561                         return list;
1562                 }});
1563         }
1564     }
1565 
1566     /**
1567      * Probes the content type of a file.
1568      *
1569      * <p> This method uses the installed {@link FileTypeDetector} implementations
1570      * to probe the given file to determine its content type. Each file type
1571      * detector's {@link FileTypeDetector#probeContentType probeContentType} is
1572      * invoked, in turn, to probe the file type. If the file is recognized then
1573      * the content type is returned. If the file is not recognized by any of the


3451      *
3452      * @throws  NotDirectoryException
3453      *          if the file could not otherwise be opened because it is not
3454      *          a directory <i>(optional specific exception)</i>
3455      * @throws  IOException
3456      *          if an I/O error occurs when opening the directory
3457      * @throws  SecurityException
3458      *          In the case of the default provider, and a security manager is
3459      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
3460      *          method is invoked to check read access to the directory.
3461      *
3462      * @see     #newDirectoryStream(Path)
3463      * @since   1.8
3464      */
3465     public static Stream<Path> list(Path dir) throws IOException {
3466         DirectoryStream<Path> ds = Files.newDirectoryStream(dir);
3467         try {
3468             final Iterator<Path> delegate = ds.iterator();
3469 
3470             // Re-wrap DirectoryIteratorException to UncheckedIOException
3471             Iterator<Path> iterator = new Iterator<Path>() {
3472                 @Override
3473                 public boolean hasNext() {
3474                     try {
3475                         return delegate.hasNext();
3476                     } catch (DirectoryIteratorException e) {
3477                         throw new UncheckedIOException(e.getCause());
3478                     }
3479                 }
3480                 @Override
3481                 public Path next() {
3482                     try {
3483                         return delegate.next();
3484                     } catch (DirectoryIteratorException e) {
3485                         throw new UncheckedIOException(e.getCause());
3486                     }
3487                 }
3488             };
3489 
3490             Spliterator<Path> spliterator =
3491                 Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT);




 385      *          StandardOpenOption#CREATE_NEW CREATE_NEW} option is specified
 386      *          <i>(optional specific exception)</i>
 387      * @throws  IOException
 388      *          if an I/O error occurs
 389      * @throws  SecurityException
 390      *          In the case of the default provider, and a security manager is
 391      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
 392      *          method is invoked to check read access to the path if the file is
 393      *          opened for reading. The {@link SecurityManager#checkWrite(String)
 394      *          checkWrite} method is invoked to check write access to the path
 395      *          if the file is opened for writing. The {@link
 396      *          SecurityManager#checkDelete(String) checkDelete} method is
 397      *          invoked to check delete access if the file is opened with the
 398      *          {@code DELETE_ON_CLOSE} option.
 399      *
 400      * @see java.nio.channels.FileChannel#open(Path,OpenOption[])
 401      */
 402     public static SeekableByteChannel newByteChannel(Path path, OpenOption... options)
 403         throws IOException
 404     {
 405         Set<OpenOption> set = new HashSet<>(options.length);
 406         Collections.addAll(set, options);
 407         return newByteChannel(path, set);
 408     }
 409 
 410     // -- Directories --
 411 
 412     private static class AcceptAllFilter
 413         implements DirectoryStream.Filter<Path>
 414     {
 415         private AcceptAllFilter() { }
 416 
 417         @Override
 418         public boolean accept(Path entry) { return true; }
 419 
 420         static final AcceptAllFilter FILTER = new AcceptAllFilter();
 421     }
 422 
 423     /**
 424      * Opens a directory, returning a {@link DirectoryStream} to iterate over
 425      * all entries in the directory. The elements returned by the directory


 499      * @throws  NotDirectoryException
 500      *          if the file could not otherwise be opened because it is not
 501      *          a directory <i>(optional specific exception)</i>
 502      * @throws  IOException
 503      *          if an I/O error occurs
 504      * @throws  SecurityException
 505      *          In the case of the default provider, and a security manager is
 506      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
 507      *          method is invoked to check read access to the directory.
 508      */
 509     public static DirectoryStream<Path> newDirectoryStream(Path dir, String glob)
 510         throws IOException
 511     {
 512         // avoid creating a matcher if all entries are required.
 513         if (glob.equals("*"))
 514             return newDirectoryStream(dir);
 515 
 516         // create a matcher and return a filter that uses it.
 517         FileSystem fs = dir.getFileSystem();
 518         final PathMatcher matcher = fs.getPathMatcher("glob:" + glob);
 519         DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<>() {
 520             @Override
 521             public boolean accept(Path entry)  {
 522                 return matcher.matches(entry.getFileName());
 523             }
 524         };
 525         return fs.provider().newDirectoryStream(dir, filter);
 526     }
 527 
 528     /**
 529      * Opens a directory, returning a {@link DirectoryStream} to iterate over
 530      * the entries in the directory. The elements returned by the directory
 531      * stream's {@link DirectoryStream#iterator iterator} are of type {@code
 532      * Path}, each one representing an entry in the directory. The {@code Path}
 533      * objects are obtained as if by {@link Path#resolve(Path) resolving} the
 534      * name of the directory entry against {@code dir}. The entries returned by
 535      * the iterator are filtered by the given {@link DirectoryStream.Filter
 536      * filter}.
 537      *
 538      * <p> When not using the try-with-resources construct, then directory
 539      * stream's {@code close} method should be invoked after iteration is


1524      *          if an I/O error occurs
1525      * @throws  SecurityException
1526      *          In the case of the default provider, and a security manager is
1527      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
1528      *          method is invoked to check read access to the file.
1529      */
1530     public static boolean isHidden(Path path) throws IOException {
1531         return provider(path).isHidden(path);
1532     }
1533 
1534     // lazy loading of default and installed file type detectors
1535     private static class FileTypeDetectors{
1536         static final FileTypeDetector defaultFileTypeDetector =
1537             createDefaultFileTypeDetector();
1538         static final List<FileTypeDetector> installedDetectors =
1539             loadInstalledDetectors();
1540 
1541         // creates the default file type detector
1542         private static FileTypeDetector createDefaultFileTypeDetector() {
1543             return AccessController
1544                 .doPrivileged(new PrivilegedAction<>() {
1545                     @Override public FileTypeDetector run() {
1546                         return sun.nio.fs.DefaultFileTypeDetector.create();
1547                 }});
1548         }
1549 
1550         // loads all installed file type detectors
1551         private static List<FileTypeDetector> loadInstalledDetectors() {
1552             return AccessController
1553                 .doPrivileged(new PrivilegedAction<>() {
1554                     @Override public List<FileTypeDetector> run() {
1555                         List<FileTypeDetector> list = new ArrayList<>();
1556                         ServiceLoader<FileTypeDetector> loader = ServiceLoader
1557                             .load(FileTypeDetector.class, ClassLoader.getSystemClassLoader());
1558                         for (FileTypeDetector detector: loader) {
1559                             list.add(detector);
1560                         }
1561                         return list;
1562                 }});
1563         }
1564     }
1565 
1566     /**
1567      * Probes the content type of a file.
1568      *
1569      * <p> This method uses the installed {@link FileTypeDetector} implementations
1570      * to probe the given file to determine its content type. Each file type
1571      * detector's {@link FileTypeDetector#probeContentType probeContentType} is
1572      * invoked, in turn, to probe the file type. If the file is recognized then
1573      * the content type is returned. If the file is not recognized by any of the


3451      *
3452      * @throws  NotDirectoryException
3453      *          if the file could not otherwise be opened because it is not
3454      *          a directory <i>(optional specific exception)</i>
3455      * @throws  IOException
3456      *          if an I/O error occurs when opening the directory
3457      * @throws  SecurityException
3458      *          In the case of the default provider, and a security manager is
3459      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
3460      *          method is invoked to check read access to the directory.
3461      *
3462      * @see     #newDirectoryStream(Path)
3463      * @since   1.8
3464      */
3465     public static Stream<Path> list(Path dir) throws IOException {
3466         DirectoryStream<Path> ds = Files.newDirectoryStream(dir);
3467         try {
3468             final Iterator<Path> delegate = ds.iterator();
3469 
3470             // Re-wrap DirectoryIteratorException to UncheckedIOException
3471             Iterator<Path> iterator = new Iterator<>() {
3472                 @Override
3473                 public boolean hasNext() {
3474                     try {
3475                         return delegate.hasNext();
3476                     } catch (DirectoryIteratorException e) {
3477                         throw new UncheckedIOException(e.getCause());
3478                     }
3479                 }
3480                 @Override
3481                 public Path next() {
3482                     try {
3483                         return delegate.next();
3484                     } catch (DirectoryIteratorException e) {
3485                         throw new UncheckedIOException(e.getCause());
3486                     }
3487                 }
3488             };
3489 
3490             Spliterator<Path> spliterator =
3491                 Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT);


< prev index next >