2

fortify is complaining about the following code:

protected void doStuff() throws Exception{
    try (CSVReader csvReader = new CSVReader(new FileReader(file))) {
        ...
        String[] read;
        read = csvReader.readNext();
        ...
    }
}

in the case that FileReader throws an exception. Im new to java, its unclear to me why the try with resources block doesnt handle the case. How to handle this?

Assafs
  • 3,257
  • 4
  • 26
  • 39
futevolei
  • 180
  • 2
  • 14

1 Answers1

0

It's always a good idea to close the lower level reader instance first and then close the wrapper level instance to avoid the memory leak.

So first close the instance of fileReader and then close instance of CSVReader.

FileReader fileReader= new FileReader(file)
CSVReader csvReader = new CSVReader(fileReader)


....
...
fileReader.close();
csvReader.close();
nagendra547
  • 5,672
  • 3
  • 29
  • 43