java.nio.file.Files.write(...)安全吗?

时间:2015-12-02 07:59:56

标签: java nio autocloseable

# -*- coding: utf-8 -*- import socks import urllib2 def urllib2_HTTP_test(): socks.set_default_proxy(socks.HTTP, "<proxy_address>", <proxy_port>) socks.wrap_module(urllib2) status = urllib2.urlopen("http://www.python.org:80").getcode() assert status == 200 if __name__ == "__main__": urllib2_HTTP_test() 方法抛出IOException

我无法在

中使用它
java.nio.file.Files.write(...)

构造

它是&#34; AutoCloseable&#34;在例外的情况下安全吗?

2 个答案:

答案 0 :(得分:4)

要使用try-with-resources,您始终需要声明并初始化实现AutoCloseable的类型的变量:

try (SomeType someType = someMethodCall()) {
}

即使您不需要在块体中引用someType。你不能简单地使用

try (someMethodCall()) {
}

在您的具体情况下,SomeType将为Path,但未实现AutoCloseable,因此您无法在try-with-resources语句中使用它。< / p>

答案 1 :(得分:2)

try-with-resources将与资源一起使用,但在这里try(java.nio.file.Files.write(...)),您只是执行写操作而不是实例化任何资源。

相关问题