properties.storeToXML(new FileOutputStream(new File(m_wcontext.getProject().getBaseDirectory() +
"/" + m_wcontext.getServiceName() + ".properties")),null);
我在10个不同的地方都有一个类似的方法调用。在代码审查中,有人建议我需要关闭资源。推荐的方法是修改以下代码:但这将使代码变得笨重,因为我需要重复10次相同的代码。
try {
fios = new FileOutputStream(new File(m_wcontext.getProject().getBaseDirectory() +
"/" + m_wcontext.getServiceName() + ".properties"));
properties.storeToXML(fios, null);
} finally {
if(fios!=null) {
fios.close();
}
}
你好,以下方法可以吗?还有更好的方法吗?
FileOutputStream fios = getFileOutPutStream(m_wcontext.getProject().getBaseDirectory() + "/" + m_wcontext.getServiceName() + ".properties");
properties.storeToXML(fios, null);
// ...
private FileOutputStream getFileOutPutStream(String path) throws IOException {
FileOutputStream fios=null;
try {
fios = new FileOutputStream(new File(m_wcontext.getProject().getBaseDirectory() +
"/" + m_wcontext.getServiceName() + ".properties"));
properties.storeToXML(fios, null);
} finally {
if(fios!=null) {
fios.close();
}
}
return fios;
}