This method may fail to clean up (close, dispose of) a stream, database object, or other resource requiring an explicit cleanup operation. In general, if a method opens a stream or other resource, the method should use a try/finally block to ensure that the stream or resource is cleaned up before the method returns.
This is the code:
public void setSource(File file) throws IOException {
m_structure = null;
setRetrieval(NONE);
if (file == null) {
throw new IOException("Source file object is null!");
}
try {
setSource(new FileInputStream(file));
}
catch (FileNotFoundException ex) {
throw new IOException("File not found");
}
m_File = file.getAbsolutePath();
}
You get the error because of the following line:
new FileInputStream(file)
You could theoretically close the inputstream inside the
setSource(InputStream is)
method however that is not the correct scope. The convention is to close a stream where you opened it. So if you change your code to the following it should remove the warning. Please note that your catch block doesn't make sense since theFileNotFoundException
is already anIOException
. There are two possibilities:Try with resources:
The pre Java 7/8 way:
If you do it like this it also means that you have to consume your
InputStream
inside thesetSource(InputStream is)
method. Otherwise it wouldn't make sense to close it...