FileWriter的正确使用,请及时关闭流

  

private static String FileWriter() {
FileWriter fw = null;
try {
fw = new FileWriter("summer");
fw.append("_heart");
} catch (Exception e) {
return e.toString();
}
return null;
}

往常我使用FileWriter都是像上面那样写的,小项目也许还看不出问题,可如果项目一大,使用太频繁,那么,就会给系统带来巨大的压力,所以,平时写代码是应该注意几十关闭。

private static String FileWriter() {
FileWriter fw = null;
try {
fw = new FileWriter("summer");
fw.append("_heart");
} catch (Exception e) {
return e.toString();
} finally {//不管成不成功都关闭
if (fw != null) {
try {
fw.close();
} catch (Exception e2) {
return e2.toString();
}
}
}
return null;
}



原文地址:https://www.cnblogs.com/xiaxinggege/p/2297751.html