サンプル

public class ClassA {

public void makeStream (File file) {

try {
FileOutputStream stream = new FileOutputStream(file);
stream.write(7);
//何らかの処理...

stream.close();

}
catch (IOException e) {
//ストリームのリーク発生...
}
}
}



ソリューション
リソース・リークを回避するには、すべての java.io.FileOutputStream で close() が呼び出されるようにします。
注: この規則は、あらゆる形式の FileOutputStream コンストラクターに適用されます。


public class ClassA {

public void makeStream (File file) {

FileOutputStream stream = null;
try {
stream = new FileOutputStream(file);
stream.write(7);
//何らかの処理...

}
catch (IOException e) {
//何らかの処理...

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

}
catch(IOException e) {
//何らかの処理...

}
}
}
}
}