java.nio.file.Files.write(...)
方法抛出 IOException
我不能用它
try(java.nio.file.Files.write(...))
建造。
它是否“自动关闭”并且在异常情况下安全?
java.nio.file.Files.write(...)
方法抛出 IOException
我不能用它
try(java.nio.file.Files.write(...))
建造。
它是否“自动关闭”并且在异常情况下安全?
要使用 try-with-resources,您总是需要声明和初始化一个实现的类型的变量AutoCloseable
:
try (SomeType someType = someMethodCall()) {
}
即使您不需要someType
在块的主体中引用。你不能简单地使用
try (someMethodCall()) {
}
在您的特定情况下,SomeType
将是Path
,它没有实现AutoCloseable
,因此无论如何您都不能在 try-with-resources 语句中使用它。
try-with-resources 将与资源一起使用,但在这里try(java.nio.file.Files.write(...))
,您只是执行写操作而不实例化任何资源。