我看过java7 try-with-resources .如果可关闭资源作为参数,我们不需要声明资源。对于这种情况,我们如何使用此功能?
public static void write(byte[] b, OutputStream os) throws Exception {
try {
os.write(b);
}
catch(Exception e) {
logger.log(Level.INFO, "Exception in writing byte array");
}
finally {
try {
if(os != null) {
os.close();
}
}catch(Exception e) {
logger.log(Level.INFO, "Exception while close the outputstream");
throw e;
}
}
}
最佳答案
你可以简单地写:
static void write(byte[] b, OutputStream os) throws Exception {
try (OutputStream o = os) {
o.write(b);
}
}
关于java - 如何使用 Java7 try-with-resources 特性将资源作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33118347/