Esempio

public class ClassA {

private FileInputStream stream;

public void makeStream (File file) {

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

}
}



Soluzione
Per evitare perdite di risorse, verificare che close() venga richiamato su ognijava.io.FileInputStream. Nota: questa regola si applica a tutte le forme del costruttore FileInputStream.


public class ClassA {

private FileInputStream stream;

public void makeStream (File file) {

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

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