Example

public class ClassA {

private FileOutputStream stream;

public void makeStream (File file) {

try {
stream = new FileOutputStream(file);
} catch(FileNotFoundException fnfe) {
//perform exception handling
}
//do something...

}
}



Solution
To avoid resource leaks, ensure that close() is invoked on every java.io.FileOutputStream. Note: This rule applies to any form of FileOutputStream constructor.


public class ClassA {

private FileOutputStream stream;

public void makeStream (File file) {

try {
stream = new FileOutputStream(file);
} catch(FileNotFoundException fnfe) {
//perform exception handling
}
//do something...

try {
stream.close();
} catch(IOException ioe) {
//perform exception handling
}
}
}