}
List<Path> list = new ArrayList<>();
- DirectoryStream<Path> ds = Files.newDirectoryStream(path);
- for (Path p : ds) {
- list.add(p);
+ DirectoryStream<Path> ds = null;
+ try {
+ ds = Files.newDirectoryStream(path);
+ for (Path p : ds) {
+ list.add(p);
+ }
+ } finally {
+ if (ds != null) {
+ ds.close();
+ }
}
+
return list;
}
*/
public static List<String> listFileName(Path path) throws IOException {
List<String> list = new ArrayList<>();
- DirectoryStream<Path> ds = Files.newDirectoryStream(path);
- for (Path p : ds) {
- list.add(p.getFileName().toString());
+ DirectoryStream<Path> ds = null;
+ try {
+ ds = Files.newDirectoryStream(path);
+ for (Path p : ds) {
+ list.add(p.getFileName().toString());
+ }
+ } finally {
+ if (ds != null) {
+ ds.close();
+ }
}
return list;