4

我在此代码段中收到有关“s”和“p”的资源泄漏警告。这个警告有效吗?

try (StringWriter s = new StringWriter(); PrintWriter p = new PrintWriter(s)) {
    p.print(InetAddress.getLocalHost().getHostName());
    p.print('/');
    Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
        NetworkInterface i = e.nextElement();
        System.out.println(i);
        if (i.getHardwareAddress() == null || i.getHardwareAddress().length == 0)
            continue;
        for (byte b : i.getHardwareAddress())
            p.printf("%02x", b);
        return s.toString();
    }
    throw new RuntimeException("Unable to determine Host ID");
}
4

1 回答 1

0

在这个片段中 - 不。除了由 JVM 管理的资源外,没有可言的资源。

于 2012-12-20T08:48:36.457 回答