jdk/src/java.base/share/classes/java/nio/file/Files.java
changeset 27339 2cf6bc8c2d2c
parent 26960 d280345c2cfb
child 27775 4ee8b208017c
child 27565 729f9700483a
equal deleted inserted replaced
27338:826d3d9a874a 27339:2cf6bc8c2d2c
   301      *
   301      *
   302      * <p> In the case of the default provider, the returned seekable byte channel
   302      * <p> In the case of the default provider, the returned seekable byte channel
   303      * is a {@link java.nio.channels.FileChannel}.
   303      * is a {@link java.nio.channels.FileChannel}.
   304      *
   304      *
   305      * <p> <b>Usage Examples:</b>
   305      * <p> <b>Usage Examples:</b>
   306      * <pre>
   306      * <pre>{@code
   307      *     Path path = ...
   307      *     Path path = ...
   308      *
   308      *
   309      *     // open file for reading
   309      *     // open file for reading
   310      *     ReadableByteChannel rbc = Files.newByteChannel(path, EnumSet.of(READ)));
   310      *     ReadableByteChannel rbc = Files.newByteChannel(path, EnumSet.of(READ)));
   311      *
   311      *
   312      *     // open file for writing to the end of an existing file, creating
   312      *     // open file for writing to the end of an existing file, creating
   313      *     // the file if it doesn't already exist
   313      *     // the file if it doesn't already exist
   314      *     WritableByteChannel wbc = Files.newByteChannel(path, EnumSet.of(CREATE,APPEND));
   314      *     WritableByteChannel wbc = Files.newByteChannel(path, EnumSet.of(CREATE,APPEND));
   315      *
   315      *
   316      *     // create file with initial permissions, opening it for both reading and writing
   316      *     // create file with initial permissions, opening it for both reading and writing
   317      *     {@code FileAttribute<Set<PosixFilePermission>> perms = ...}
   317      *     FileAttribute<Set<PosixFilePermission>> perms = ...
   318      *     SeekableByteChannel sbc = Files.newByteChannel(path, EnumSet.of(CREATE_NEW,READ,WRITE), perms);
   318      *     SeekableByteChannel sbc =
   319      * </pre>
   319      *         Files.newByteChannel(path, EnumSet.of(CREATE_NEW,READ,WRITE), perms);
       
   320      * }</pre>
   320      *
   321      *
   321      * @param   path
   322      * @param   path
   322      *          the path to the file to open or create
   323      *          the path to the file to open or create
   323      * @param   options
   324      * @param   options
   324      *          options specifying how the file is opened
   325      *          options specifying how the file is opened
  1700      *    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
  1701      *    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
  1701      * </pre>
  1702      * </pre>
  1702      * Alternatively, suppose we want to read file's POSIX attributes without
  1703      * Alternatively, suppose we want to read file's POSIX attributes without
  1703      * following symbolic links:
  1704      * following symbolic links:
  1704      * <pre>
  1705      * <pre>
  1705      *    PosixFileAttributes attrs = Files.readAttributes(path, PosixFileAttributes.class, NOFOLLOW_LINKS);
  1706      *    PosixFileAttributes attrs =
       
  1707      *        Files.readAttributes(path, PosixFileAttributes.class, NOFOLLOW_LINKS);
  1706      * </pre>
  1708      * </pre>
  1707      *
  1709      *
  1708      * @param   <A>
  1710      * @param   <A>
  1709      *          The {@code BasicFileAttributes} type
  1711      *          The {@code BasicFileAttributes} type
  1710      * @param   path
  1712      * @param   path
  2838      *          options specifying how the file is opened
  2840      *          options specifying how the file is opened
  2839      *
  2841      *
  2840      * @return  a new buffered writer, with default buffer size, to write text
  2842      * @return  a new buffered writer, with default buffer size, to write text
  2841      *          to the file
  2843      *          to the file
  2842      *
  2844      *
       
  2845      * @throws  IllegalArgumentException
       
  2846      *          if {@code options} contains an invalid combination of options
  2843      * @throws  IOException
  2847      * @throws  IOException
  2844      *          if an I/O error occurs opening or creating the file
  2848      *          if an I/O error occurs opening or creating the file
  2845      * @throws  UnsupportedOperationException
  2849      * @throws  UnsupportedOperationException
  2846      *          if an unsupported option is specified
  2850      *          if an unsupported option is specified
  2847      * @throws  SecurityException
  2851      * @throws  SecurityException
  2878      *          options specifying how the file is opened
  2882      *          options specifying how the file is opened
  2879      *
  2883      *
  2880      * @return  a new buffered writer, with default buffer size, to write text
  2884      * @return  a new buffered writer, with default buffer size, to write text
  2881      *          to the file
  2885      *          to the file
  2882      *
  2886      *
       
  2887      * @throws  IllegalArgumentException
       
  2888      *          if {@code options} contains an invalid combination of options
  2883      * @throws  IOException
  2889      * @throws  IOException
  2884      *          if an I/O error occurs opening or creating the file
  2890      *          if an I/O error occurs opening or creating the file
  2885      * @throws  UnsupportedOperationException
  2891      * @throws  UnsupportedOperationException
  2886      *          if an unsupported option is specified
  2892      *          if an unsupported option is specified
  2887      * @throws  SecurityException
  2893      * @throws  SecurityException
  2889      *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
  2895      *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
  2890      *          method is invoked to check write access to the file.
  2896      *          method is invoked to check write access to the file.
  2891      *
  2897      *
  2892      * @since 1.8
  2898      * @since 1.8
  2893      */
  2899      */
  2894     public static BufferedWriter newBufferedWriter(Path path, OpenOption... options) throws IOException {
  2900     public static BufferedWriter newBufferedWriter(Path path, OpenOption... options)
       
  2901         throws IOException
       
  2902     {
  2895         return newBufferedWriter(path, StandardCharsets.UTF_8, options);
  2903         return newBufferedWriter(path, StandardCharsets.UTF_8, options);
  2896     }
  2904     }
  2897 
  2905 
  2898     /**
  2906     /**
  2899      * Reads all bytes from an input stream and writes them to an output stream.
  2907      * Reads all bytes from an input stream and writes them to an output stream.
  3271      * @param   options
  3279      * @param   options
  3272      *          options specifying how the file is opened
  3280      *          options specifying how the file is opened
  3273      *
  3281      *
  3274      * @return  the path
  3282      * @return  the path
  3275      *
  3283      *
       
  3284      * @throws  IllegalArgumentException
       
  3285      *          if {@code options} contains an invalid combination of options
  3276      * @throws  IOException
  3286      * @throws  IOException
  3277      *          if an I/O error occurs writing to or creating the file
  3287      *          if an I/O error occurs writing to or creating the file
  3278      * @throws  UnsupportedOperationException
  3288      * @throws  UnsupportedOperationException
  3279      *          if an unsupported option is specified
  3289      *          if an unsupported option is specified
  3280      * @throws  SecurityException
  3290      * @throws  SecurityException
  3328      * @param   options
  3338      * @param   options
  3329      *          options specifying how the file is opened
  3339      *          options specifying how the file is opened
  3330      *
  3340      *
  3331      * @return  the path
  3341      * @return  the path
  3332      *
  3342      *
       
  3343      * @throws  IllegalArgumentException
       
  3344      *          if {@code options} contains an invalid combination of options
  3333      * @throws  IOException
  3345      * @throws  IOException
  3334      *          if an I/O error occurs writing to or creating the file, or the
  3346      *          if an I/O error occurs writing to or creating the file, or the
  3335      *          text cannot be encoded using the specified charset
  3347      *          text cannot be encoded using the specified charset
  3336      * @throws  UnsupportedOperationException
  3348      * @throws  UnsupportedOperationException
  3337      *          if an unsupported option is specified
  3349      *          if an unsupported option is specified
  3374      * @param   options
  3386      * @param   options
  3375      *          options specifying how the file is opened
  3387      *          options specifying how the file is opened
  3376      *
  3388      *
  3377      * @return  the path
  3389      * @return  the path
  3378      *
  3390      *
       
  3391      * @throws  IllegalArgumentException
       
  3392      *          if {@code options} contains an invalid combination of options
  3379      * @throws  IOException
  3393      * @throws  IOException
  3380      *          if an I/O error occurs writing to or creating the file, or the
  3394      *          if an I/O error occurs writing to or creating the file, or the
  3381      *          text cannot be encoded as {@code UTF-8}
  3395      *          text cannot be encoded as {@code UTF-8}
  3382      * @throws  UnsupportedOperationException
  3396      * @throws  UnsupportedOperationException
  3383      *          if an unsupported option is specified
  3397      *          if an unsupported option is specified
  3450         DirectoryStream<Path> ds = Files.newDirectoryStream(dir);
  3464         DirectoryStream<Path> ds = Files.newDirectoryStream(dir);
  3451         try {
  3465         try {
  3452             final Iterator<Path> delegate = ds.iterator();
  3466             final Iterator<Path> delegate = ds.iterator();
  3453 
  3467 
  3454             // Re-wrap DirectoryIteratorException to UncheckedIOException
  3468             // Re-wrap DirectoryIteratorException to UncheckedIOException
  3455             Iterator<Path> it = new Iterator<Path>() {
  3469             Iterator<Path> iterator = new Iterator<Path>() {
  3456                 @Override
  3470                 @Override
  3457                 public boolean hasNext() {
  3471                 public boolean hasNext() {
  3458                     try {
  3472                     try {
  3459                         return delegate.hasNext();
  3473                         return delegate.hasNext();
  3460                     } catch (DirectoryIteratorException e) {
  3474                     } catch (DirectoryIteratorException e) {
  3469                         throw new UncheckedIOException(e.getCause());
  3483                         throw new UncheckedIOException(e.getCause());
  3470                     }
  3484                     }
  3471                 }
  3485                 }
  3472             };
  3486             };
  3473 
  3487 
  3474             return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.DISTINCT), false)
  3488             Spliterator<Path> spliterator =
       
  3489                 Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT);
       
  3490             return StreamSupport.stream(spliterator, false)
  3475                                 .onClose(asUncheckedRunnable(ds));
  3491                                 .onClose(asUncheckedRunnable(ds));
  3476         } catch (Error|RuntimeException e) {
  3492         } catch (Error|RuntimeException e) {
  3477             try {
  3493             try {
  3478                 ds.close();
  3494                 ds.close();
  3479             } catch (IOException ex) {
  3495             } catch (IOException ex) {
  3570                                     FileVisitOption... options)
  3586                                     FileVisitOption... options)
  3571         throws IOException
  3587         throws IOException
  3572     {
  3588     {
  3573         FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options);
  3589         FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options);
  3574         try {
  3590         try {
  3575             return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT), false)
  3591             Spliterator<FileTreeWalker.Event> spliterator =
       
  3592                 Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT);
       
  3593             return StreamSupport.stream(spliterator, false)
  3576                                 .onClose(iterator::close)
  3594                                 .onClose(iterator::close)
  3577                                 .map(entry -> entry.file());
  3595                                 .map(entry -> entry.file());
  3578         } catch (Error|RuntimeException e) {
  3596         } catch (Error|RuntimeException e) {
  3579             iterator.close();
  3597             iterator.close();
  3580             throw e;
  3598             throw e;
  3683                                     FileVisitOption... options)
  3701                                     FileVisitOption... options)
  3684         throws IOException
  3702         throws IOException
  3685     {
  3703     {
  3686         FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options);
  3704         FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options);
  3687         try {
  3705         try {
  3688             return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT), false)
  3706             Spliterator<FileTreeWalker.Event> spliterator =
       
  3707                 Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT);
       
  3708             return StreamSupport.stream(spliterator, false)
  3689                                 .onClose(iterator::close)
  3709                                 .onClose(iterator::close)
  3690                                 .filter(entry -> matcher.test(entry.file(), entry.attributes()))
  3710                                 .filter(entry -> matcher.test(entry.file(), entry.attributes()))
  3691                                 .map(entry -> entry.file());
  3711                                 .map(entry -> entry.file());
  3692         } catch (Error|RuntimeException e) {
  3712         } catch (Error|RuntimeException e) {
  3693             iterator.close();
  3713             iterator.close();