Beispiel
public class
ClassA {
private
FileOutputStream stream;
public void
makeStream (File file) {
try
{
stream =
new
FileOutputStream(file);
}
catch
(FileNotFoundException fnfe) {
//perform exception handling
}
//do something...
}
}
Lösung
Um Ressourcenlecks zu vermeiden, stellen Sie sicher, dass die Methode 'close()' für jedes Vorkommen von 'java.io.FileOutputStream' aufgerufen wird. Hinweis: Diese Regel gilt für jede Form des Konstruktors 'FileOutputStream'.
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
}
}
}