diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/RecursiveDirectoryScanner.java b/spring-integration-file/src/main/java/org/springframework/integration/file/RecursiveDirectoryScanner.java index 8657ff8ade..dc32bb0d3d 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/RecursiveDirectoryScanner.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/RecursiveDirectoryScanner.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 the original author or authors. + * Copyright 2017-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -70,12 +70,13 @@ public class RecursiveDirectoryScanner extends DefaultDirectoryScanner { public List listFiles(File directory) throws IllegalArgumentException { FileListFilter filter = getFilter(); boolean supportAcceptFilter = filter instanceof AbstractFileListFilter; - try { - Stream fileStream = Files.walk(directory.toPath(), this.maxDepth, this.fileVisitOptions) - .skip(1) - .map(Path::toFile) - .filter(file -> !supportAcceptFilter - || ((AbstractFileListFilter) filter).accept(file)); + try (Stream pathStream = Files.walk(directory.toPath(), this.maxDepth, this.fileVisitOptions);) { + Stream fileStream = + pathStream + .skip(1) + .map(Path::toFile) + .filter(file -> !supportAcceptFilter + || ((AbstractFileListFilter) filter).accept(file)); if (supportAcceptFilter) { return fileStream.collect(Collectors.toList());