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