Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sonar scan says Use try-with-resources or close this "Stream" in a "finally" clause

Sonar qube is giving me the following error.
Use try-with-resources or close this "Stream" in a "finally" clause. this is my code.

Path start = Paths.get(filePath);
Stream<File> stream;
try {
    stream = Files.walk(start, 1, FileVisitOption.FOLLOW_LINKS).map(s -> s.toFile());
    List<File> files = stream.collect(Collectors.toList());
                    files.remove(0);
                    for (File f : files) {
                        String fileName = f.toPath().getFileName().toString();
                        if (fileName.matches(regex)) {
                            fileList.add(f.toPath().toString());
                        }
                    }
    } catch (IOException e) {
}

How can I fixed this error. Thanks in Advanced!!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

define and open your stream this way:

try (Stream<File> stream = Files.walk(start, 1, FileVisitOption.FOLLOW_LINKS).map(s -> s.toFile())){

Doing this, the system will automatically close the stream and you don’t need to worry about it

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading