Is this a memory leak or a false positive?

后端 未结 4 517
谎友^
谎友^ 2020-12-01 18:06

This is my code:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class temp         


        
4条回答
  •  醉酒成梦
    2020-12-01 18:13

    What is giving you this warning? Presumably it warns because the allocation/closing isn't done in a try/finally block, which generally is a bad idea (in this specific case not a problem though because the only thing that can throw an error is the new FileReader and if it throws no resource was actually allocated - but that can change with a single method call..)

    Closing a scanner closes the underlying stream (to be exact anything that implements itself Closeable (yes BufferedReader does) so the code is fine apart from that.

提交回复
热议问题