【发布时间】:2016-08-06 11:12:52
【问题描述】:
我正在使用以下代码提取一些数据并将它们保存到 CSV 文件。但现在的问题是程序将数据写入 CSV 文件,并且每当代码再次运行时,它就会被删除,旧的结果将被新的结果替换! 问题是,有没有办法保存结果并将新结果存储在新行中?如果是,那该怎么做?
这是我的代码:
private void writeReport() {
BufferedWriter out = null;
try {
FileWriter fstream = new FileWriter("out.csv");
out = new BufferedWriter(fstream);
out.write("File Name;");
out.write("Total Lines of Code;");
out.write("Executable Lines;");
out.write("Lines of Comments;");
out.write("Trivial Lines;");
out.write("Empty Lines;");
out.write("Code Complexity;");
out.write("Number of Files;"); //to be changed to numver of files
out.write("Average File Complexity;"); //to be changed to averag file complexity
out.write("Comment Percentage;"); //total
out.write("Total Lines of Test Code;");
out.write("Total Comments in Tests;");
out.write("Total Trivial Lines in Tests;");
out.write("Total Empty Lines in Tests;");
out.write("Total Number of Test Files;");
out.write("Comment Presentage in Test;");
out.write(System.getProperty("line.separator"));
// for (int i = 0; i < newFiles.getNrOfFiles(); i++) {
out.write("test" + ";");
// out.write(newFiles.getParser(i).getSourceFile().getName()+ ";");
out.write(String.valueOf(newFiles.sumLinesOfCode()) + ";");
out.write(String.valueOf(newFiles.sumLinesOfStatements()) + ";");
out.write(String.valueOf(newFiles.sumLinesOfComments()) + ";");
out.write(String.valueOf(newFiles.sumTrivialLines()) + ";");
out.write(String.valueOf(newFiles.sumEmptyLines()) + ";");
out.write(String.valueOf(newFiles.sumComplexity())+ ";");
out.write(String.valueOf(newFiles.getNrOfFiles()) + ";");
out.write(String.valueOf(newFiles.sumAvgComplexity()) + ";");
out.write(String.valueOf((100 * newFiles.sumLinesOfComments()) / newFiles.sumLinesOfCode() + "%") + ";");
out.write(System.getProperty("line.separator"));
//Close the output stream
out.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
// return;
}
}
【问题讨论】:
-
new FileWriter("out.csv", true);怎么样?它将附加到当前文件。