equal
deleted
inserted
replaced
52 * resources associated with the stream. Failure to close the stream may result |
52 * resources associated with the stream. Failure to close the stream may result |
53 * in a resource leak. The try-with-resources statement provides a useful |
53 * in a resource leak. The try-with-resources statement provides a useful |
54 * construct to ensure that the stream is closed: |
54 * construct to ensure that the stream is closed: |
55 * <pre> |
55 * <pre> |
56 * Path dir = ... |
56 * Path dir = ... |
57 * try (DirectoryStream<Path> stream = dir.newDirectoryStream()) { |
57 * try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) { |
58 * for (Path entry: stream) { |
58 * for (Path entry: stream) { |
59 * ... |
59 * ... |
60 * } |
60 * } |
61 * } |
61 * } |
62 * </pre> |
62 * </pre> |
95 * <p> <b>Usage Examples:</b> |
95 * <p> <b>Usage Examples:</b> |
96 * Suppose we want a list of the source files in a directory. This example uses |
96 * Suppose we want a list of the source files in a directory. This example uses |
97 * both the for-each and try-with-resources constructs. |
97 * both the for-each and try-with-resources constructs. |
98 * <pre> |
98 * <pre> |
99 * List<Path> listSourceFiles(Path dir) throws IOException { |
99 * List<Path> listSourceFiles(Path dir) throws IOException { |
100 * List<Path> result = new ArrayList<Path>(); |
100 * List<Path> result = new ArrayList<>(); |
101 * try (DirectoryStream<Path> stream = dir.newDirectoryStream("*.{c,h,cpp,hpp,java}")) { |
101 * try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{c,h,cpp,hpp,java}")) { |
102 * for (Path entry: stream) { |
102 * for (Path entry: stream) { |
103 * result.add(entry); |
103 * result.add(entry); |
104 * } |
104 * } |
105 * } catch (DirectoryIteratorException ex) { |
105 * } catch (DirectoryIteratorException ex) { |
106 * // I/O error encounted during the iteration, the cause is an IOException |
106 * // I/O error encounted during the iteration, the cause is an IOException |
111 * </pre> |
111 * </pre> |
112 * @param <T> The type of element returned by the iterator |
112 * @param <T> The type of element returned by the iterator |
113 * |
113 * |
114 * @since 1.7 |
114 * @since 1.7 |
115 * |
115 * |
116 * @see Path#newDirectoryStream |
116 * @see Files#newDirectoryStream(Path) |
117 */ |
117 */ |
118 |
118 |
119 public interface DirectoryStream<T> |
119 public interface DirectoryStream<T> |
120 extends Closeable, Iterable<T> |
120 extends Closeable, Iterable<T> |
121 { |
121 { |
122 /** |
122 /** |
123 * An interface that is implemented by objects that decide if a directory |
123 * An interface that is implemented by objects that decide if a directory |
124 * entry should be accepted or filtered. A {@code Filter} is passed as the |
124 * entry should be accepted or filtered. A {@code Filter} is passed as the |
125 * parameter to the {@link Path#newDirectoryStream(DirectoryStream.Filter) |
125 * parameter to the {@link Files#newDirectoryStream(Path,DirectoryStream.Filter)} |
126 * newDirectoryStream} method when opening a directory to iterate over the |
126 * method when opening a directory to iterate over the entries in the |
127 * entries in the directory. |
127 * directory. |
128 * |
128 * |
129 * @param <T> the type of the directory entry |
129 * @param <T> the type of the directory entry |
130 * |
130 * |
131 * @since 1.7 |
131 * @since 1.7 |
132 */ |
132 */ |