LUCENE-10144:fix resource leak due to Files.list (#354)

* LUCENE-10144:fix resource leak due to Files.list

* LUCENE-10144:fix resource leak due to Files.list
This commit is contained in:
lujiefsi 2023-11-02 18:53:48 +08:00 committed by GitHub
parent cdc7d87fcc
commit cbb5b6e331
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 3 deletions

View File

@ -27,6 +27,7 @@ import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
/**
* Split the Reuters SGML documents into Simple Text files containing:
@ -44,9 +45,10 @@ public class ExtractReuters {
public void extract() throws IOException {
long count = 0;
Files.createDirectories(outputDir);
if (Files.list(outputDir).count() > 0) {
throw new IOException("The output directory must be empty: " + outputDir);
try(Stream<Path> files = Files.list(outputDir)) {
if (files.count() > 0) {
throw new IOException("The output directory must be empty: " + outputDir);
}
}
try (DirectoryStream<Path> stream = Files.newDirectoryStream(reutersDir, "*.sgm")) {