< prev index next >

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

Print this page




  86  * <p> The elements returned by the iterator are in no specific order. Some file
  87  * systems maintain special links to the directory itself and the directory's
  88  * parent directory. Entries representing these links are not returned by the
  89  * iterator.
  90  *
  91  * <p> The iterator is <i>weakly consistent</i>. It is thread safe but does not
  92  * freeze the directory while iterating, so it may (or may not) reflect updates
  93  * to the directory that occur after the {@code DirectoryStream} is created.
  94  *
  95  * <p> <b>Usage Examples:</b>
  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.
  98  * <pre>
  99  *   List&lt;Path&gt; listSourceFiles(Path dir) throws IOException {
 100  *       List&lt;Path&gt; result = new ArrayList&lt;&gt;();
 101  *       try (DirectoryStream&lt;Path&gt; stream = Files.newDirectoryStream(dir, "*.{c,h,cpp,hpp,java}")) {
 102  *           for (Path entry: stream) {
 103  *               result.add(entry);
 104  *           }
 105  *       } catch (DirectoryIteratorException ex) {
 106  *           // I/O error encounted during the iteration, the cause is an IOException
 107  *           throw ex.getCause();
 108  *       }
 109  *       return result;
 110  *   }
 111  * </pre>
 112  * @param   <T>     The type of element returned by the iterator
 113  *
 114  * @since 1.7
 115  *
 116  * @see Files#newDirectoryStream(Path)
 117  */
 118 
 119 public interface DirectoryStream<T>
 120     extends Closeable, Iterable<T> {
 121     /**
 122      * An interface that is implemented by objects that decide if a directory
 123      * entry should be accepted or filtered. A {@code Filter} is passed as the
 124      * parameter to the {@link Files#newDirectoryStream(Path,DirectoryStream.Filter)}
 125      * method when opening a directory to iterate over the entries in the
 126      * directory.




  86  * <p> The elements returned by the iterator are in no specific order. Some file
  87  * systems maintain special links to the directory itself and the directory's
  88  * parent directory. Entries representing these links are not returned by the
  89  * iterator.
  90  *
  91  * <p> The iterator is <i>weakly consistent</i>. It is thread safe but does not
  92  * freeze the directory while iterating, so it may (or may not) reflect updates
  93  * to the directory that occur after the {@code DirectoryStream} is created.
  94  *
  95  * <p> <b>Usage Examples:</b>
  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.
  98  * <pre>
  99  *   List&lt;Path&gt; listSourceFiles(Path dir) throws IOException {
 100  *       List&lt;Path&gt; result = new ArrayList&lt;&gt;();
 101  *       try (DirectoryStream&lt;Path&gt; stream = Files.newDirectoryStream(dir, "*.{c,h,cpp,hpp,java}")) {
 102  *           for (Path entry: stream) {
 103  *               result.add(entry);
 104  *           }
 105  *       } catch (DirectoryIteratorException ex) {
 106  *           // I/O error encountered during the iteration, the cause is an IOException
 107  *           throw ex.getCause();
 108  *       }
 109  *       return result;
 110  *   }
 111  * </pre>
 112  * @param   <T>     The type of element returned by the iterator
 113  *
 114  * @since 1.7
 115  *
 116  * @see Files#newDirectoryStream(Path)
 117  */
 118 
 119 public interface DirectoryStream<T>
 120     extends Closeable, Iterable<T> {
 121     /**
 122      * An interface that is implemented by objects that decide if a directory
 123      * entry should be accepted or filtered. A {@code Filter} is passed as the
 124      * parameter to the {@link Files#newDirectoryStream(Path,DirectoryStream.Filter)}
 125      * method when opening a directory to iterate over the entries in the
 126      * directory.


< prev index next >