Esempio

public class ClassA {

public void makeStream (OutputStream oStream) {

try {
DataOutputStream stream = new DataOutputStream(oStream);
stream.write(7);
//eseguire un'operazione...

stream.close();

}
catch (IOException e) {
//flusso mancante...
}
}
}



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


public class ClassA {

public void makeStream (OutputStream oStream) {

DataOutputStream stream = new DataOutputStream(oStream);
try {
stream.write(7);
//eseguire un'operazione...

}
catch (IOException e) {
//eseguire un'operazione...

}
finally {
if (stream != null) {
try {
stream.close();

}
catch(IOException e) {
//eseguire un'operazione...

}
}
}
}
}