在下面的代码中,我尝试访问采用 InputStream 的其他构造函数...但是我需要以某种方式关闭此流以避免资源泄漏。如果我尝试尝试捕获,它会抱怨构造函数调用不是第一个语句。有没有什么方法可以通过这个 InputStream 而没有任何潜在的风险?
public Input(File source) throws FileNotFoundException {
this(new FileInputStream(source));
}
在下面的代码中,我尝试访问采用 InputStream 的其他构造函数...但是我需要以某种方式关闭此流以避免资源泄漏。如果我尝试尝试捕获,它会抱怨构造函数调用不是第一个语句。有没有什么方法可以通过这个 InputStream 而没有任何潜在的风险?
public Input(File source) throws FileNotFoundException {
this(new FileInputStream(source));
}
您需要实现此类AutoClosable
并确保在 try-with-resource 中使用它:
public class Input extends SomeClass implements AutoCloseable {
public Input(File source) throws FileNotFoundException {
this(new FileInputStream(source));
}
@Override
public void close() {
someMethodThatClosesInnerResource();
}
}
然后,您可以像这样使用对象:
try (Input input = new Input(source)) {
...
}
如果您担心超级构造函数会抛出异常,那么您可能需要添加一个构建器方法来防止早期异常:
public class Input extends SomeClass implements AutoCloseable {
public static Input createInput(File source) throws Exception {
FileInputStream inputstream = new FileInputStream(source);
try {
return new Input(inputstream);
} catch (Exception e) {
inputstream.close();
throw e;
}
}
private Input(FileInputStream source) {
this(source);
}
@Override
public void close() {
someMethodThatClosesInnerResource();
}
}
然后,您仍应将其用作 try-with-resource:
try (Input input = Input.createInput(source)) {
...
}
请注意,在构建器方法中,您必须防止所有异常...这不是很好的做法,因为现在您必须使该方法抛出一般异常...或使用实用程序静默抛出。您可以通过不让构造函数创建FileInputStream
. 相反,只需将其FileInputStream
注入您的类并在 try-with-resource 中创建它。这将安全地允许在发生 ctor 异常时关闭两个对象:
try (
FileInputStream stream = new FileInputStream(source);
Input input = new Input(stream)
) {
...
}