I have closed file steam in try/finally, but code analysis warns me:
- Possible failure to close a FileOutputStream
- Possible failure to close a PrintWriter
- Possible failure to close an OutputStreamWriter
How can failure happen? How can I ensure the FileStream is closed?
public void writeFile(String filepath)
{
BufferedWriter bw = null;
PrintWriter pw = null;
try {
File file = new File(filepath);
bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
pw = new PrintWriter(bfw);
//do something
}catch(Exception e){
e.printStackTrace();
}
finally{
try{
bfw.close();
pw.close();
}catch(Exception e){
e.printStackTrace();
}
}
}