2012-12-20 55 views
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"); 
} 
+1

在Eclipse的編譯器中可能是錯誤的?嘗試在try塊外拋出異常 – Adrian

+0

這就是我所懷疑的,但嘗試與資源對我來說仍然有點新,所以我想三重檢查。 –

+0

也許你可以在Netbeans或IntelliJ IDEA中打開你的代碼,並檢查他們說什麼。 – Adrian

回答

0

在這個片段 - 沒有。除了由JVM管理的資源之外,沒有資源可以談論。