0

I'm developing a CLOC program, that traverse into other program's source code and takes the values from them!, where its saving some values into CSV file using:

out.write 

the problem is that as much as I run the program as much as the values will be written in the file, despite if they are already there or not!

my question is how to tell the program to write ONLY the values that are NOT already available!

here is my code:

 private void writeReport() {
    BufferedWriter out = null;
    try {
        File file = new File("out.csv");
        boolean exists = file.exists();
        FileWriter fstream = new FileWriter(file, exists /*=append*/);
        out = new BufferedWriter(fstream);
        if (!exists) {
            out.write("Project Name;");
            out.write("Version;");
            out.write("Download Date;");
            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("Total Comment Percentage;"); //total

            out.write("Total Lines of Test Code;");
            out.write("Excutable Test Lines;");
            out.write("Lines of Comments in Tests;");
            out.write("Trivial Lines in Tests;");
            out.write("Empty Lines in Tests;");
            out.write("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.getProjectName()) + ";");
        out.write(String.valueOf(newFiles.getVersionNumber()) + ";");
        out.write(String.valueOf(newFiles.getDownloadDate()) + ";");
        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(String.valueOf(testFiles.sumLinesOfCode()) + ";");
        out.write(String.valueOf(testFiles.sumLinesOfStatements()) + ";");
        out.write(String.valueOf(testFiles.sumLinesOfComments()) + ";");
        out.write(String.valueOf(testFiles.sumTrivialLines()) + ";");
        out.write(String.valueOf(testFiles.sumEmptyLines()) + ";");
        out.write(String.valueOf(testFiles.getNrOfFiles()) + ";");
        out.write(String.valueOf((100 * testFiles.sumLinesOfComments()) / testFiles.sumLinesOfCode() + "%") + ";");

        out.write(System.getProperty("line.separator"));

      //  }

        //Close the output stream
        out.close();
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
        //  return;
    }
}

So, how can I do that?

Best regards

user5923402
  • 217
  • 3
  • 12
  • why don't you delete the old file and write it new instead of appending it to one file? – osanger Apr 20 '16 at 09:34
  • Read the existing file .. compare what you have with the contents read and write only what needs to be written – Sanjeev Apr 20 '16 at 09:34
  • @dr_debug I have thougt about that, but its not really practical solution for many reasons – user5923402 Apr 20 '16 at 09:35
  • 1
    if your data doesn't have a time-stamp or a unique id you have to read the written data again (e.g. to a hashMap) and compare them to the new data. – osanger Apr 20 '16 at 09:39

0 Answers0