【发布时间】:2017-05-26 12:26:03
【问题描述】:
我遇到了 close the "FileOutputStream" from sonar 的麻烦。尽管我关闭了文件。从声纳的文件中,我不明白这个错误。 我看了帖子 SONAR issue - Close this FileInputStream。 这也不能解决我的问题。
public void trainL2lSupport(String training_path, String model_path) throws Exception {
BasicConfigurator.configure();
String[] options = { "-s 1" };
FileOutputStream ms = new FileOutputStream(model_path); // This one is producing the error.
classifier.setOptions(options);
logger.info(msg + classifier.globalInfo());
loader.setFile(new File(training_path));
Instances data_set = loader.getDataSet();
data_set.setClassIndex(data_set.numAttributes() - 1);
classifier.buildClassifier(data_set);
Evaluation evaluation = new Evaluation(data_set);
evaluation.crossValidateModel(classifier, data_set, 40, new Random(1));
logger.info(evaluation.toSummaryString());
logger.info(msg1 + timer.stop());
// oos = new ObjectOutputStream(ms);
try {
ObjectOutputStream oos = new ObjectOutputStream(ms);
oos.writeObject(classifier);
oos.flush();
oos.close();
logger.info(msg3+ evaluation.toSummaryString());
logger.info(msg1 + timer.stop());
logger.info("File closed safetly");
} catch(Exception e) {
}
finally {
ms.close();
}
}
如何解决?
【问题讨论】:
-
将 declalation 放入 try 中,因为在 try FileOutputStream ms = new FileOutputStream(model_path); 之前不使用对象 ms // 这个正在产生错误。