这是内存泄漏还是误报? [英] Is this a memory leak or a false positive?

查看:34
本文介绍了这是内存泄漏还是误报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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

public class temp {
    public static void main(String[] args) throws FileNotFoundException {
        BufferedReader a = new BufferedReader(new FileReader("a"));
        Scanner scanner = new Scanner(a).useDelimiter(",");
        scanner.close();
    }
}

我在 new Scanner(a) 收到警告说(我正在用 jdk1.7.0_05 编译.):

I get a warning at new Scanner(a) that says (I'm compiling with jdk1.7.0_05.):

Resource leak: '<unassigned Closeable value>' is never closed.

我做错了什么,还是只是一个错误的警告?

Am I doing something wrong, or is this just a false warning?

推荐答案

这样拆分代码,警告会消失吗?

If you split the code like this, will the warning go away?

  Scanner scanner = new Scanner(a);
  scanner.useDelimiter(",");
  scanner.close();

这篇关于这是内存泄漏还是误报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆