Java Code Examples for org.apache.commons.csv.CSVPrinter#flush()

The following examples show how to use org.apache.commons.csv.CSVPrinter#flush() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RepairEvaluator.java    From coming with MIT License 7 votes vote down vote up
private void dumpCSV(String csvFileName, List<Ranking> rankings) {
    List<String> header = new ArrayList<>();
    header.add("entryName");
    header.add("number");
    header.add("median");
    header.add("mean");
    header.add("SD");
    try {
        BufferedWriter writer = java.nio.file.Files.newBufferedWriter(Paths.get(csvFileName));
        CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(header.toArray(new String[0])));
        rankings.sort(Comparator.comparing(Ranking::getMean).thenComparing(Ranking::getSD).thenComparing(Ranking::getEntryName));
        for (Ranking ranking : rankings) {
            csvPrinter.printRecord(ranking.getValues());
        }
        csvPrinter.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: JarRchedWriter.java    From Decca with MIT License 6 votes vote down vote up
public void writeCsv(String outPath, boolean append) {
	try {
		// final String[] header = { "projectId", "conflictId", "type", "origin",
		// "load", "other" };
		// CSVFormat format = CSVFormat.DEFAULT.withHeader(header);

		CSVPrinter printer = new CSVPrinter(new FileWriter(outPath, append), CSVFormat.DEFAULT);
		for (NodeConflict conflict : NodeConflicts.i().getConflicts()) {
			FourRow fourRow = conflict.getRiskAna().getFourRow();
			printer.printRecord(fourRow.mthdRow);
			printer.printRecord(fourRow.mthdNameRow);
			printer.printRecord(fourRow.serviceRow);
			printer.printRecord(fourRow.serviceNameRow);
			printer.flush();
		}
		printer.close();
	} catch (Exception e) {
		MavenUtil.i().getLog().error("can't write risk result:", e);
	}
}
 
Example 3
Source File: ImportantChemicalsWikipedia.java    From act with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This function writes the important chemicals set to a TSV file.
 * @param outputPath a String indicating where the file should be written (including its name)
 */
public void writeToTSV(String outputPath) {
  try {
    BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath));
    CSVPrinter printer = new CSVPrinter(writer, TSV_FORMAT);
    printer.printComment("This file has been generated by the ImportantChemicalsWikipedia.java script.");
    printer.printComment("Format: WIKIPEDIA<tab><wikipedia url><tab><inchi><tab><metadata>");
    for (ImportantChemical importantChemical : importantChemicalsWikipedia) {
      List<String> nextLine = new ArrayList<>();
      nextLine.add(importantChemical.getType());
      nextLine.add(importantChemical.getDbid());
      nextLine.add(importantChemical.getInchi());
      nextLine.add(mapper.writeValueAsString(importantChemical.getMetadata()));
      printer.printRecord(nextLine);
    }
    printer.flush();
    writer.close();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 4
Source File: CQL2CSV.java    From cqlkit with Apache License 2.0 5 votes vote down vote up
@Override
protected void head(ColumnDefinitions columnDefinitions, PrintStream out) {
    definitions = columnDefinitions.asList().toArray(new ColumnDefinitions.Definition[]{});
    csvFormat = CSVFormat.DEFAULT;

    // Print the header
    if (!commandLine.hasOption("H")) {
        List<String> list = columnDefinitions.asList().stream()
                .map(col -> col.getName())
                .collect(Collectors.toList());
        if (commandLine.hasOption("l")) {
            list.add(0, "linenumber");
        }
        if (commandLine.hasOption("queryKeys")) {
            list.add("tokenPercentage");
        }

        try {
            CSVPrinter print = csvFormat
                    .withHeader(list.toArray(new String[]{}))
                    .print(out);
            print.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example 5
Source File: AdminAnalyticsKillMapsApi.java    From CodeDefenders with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns a CSV file containing the killmap analytics data.
 * The returned CSV will have a header.
 */
private void doGetCSV(HttpServletResponse response) throws IOException {
    response.setContentType("text/csv");

    List<KillmapDataDTO> killmapData = AnalyticsDAO.getAnalyticsKillMapData();

    String[] columns = new String[]{
        "userId",
        "userName",
        "classId",
        "className",
        "role",
        "usefulMutants",
        "usefulTests"
    };

    PrintWriter out = response.getWriter();
    CSVPrinter csvPrinter = new CSVPrinter(out, CSVFormat.DEFAULT.withHeader(columns));

    for (KillmapDataDTO k : killmapData) {
        for(String column : columns) {
            try {
                csvPrinter.print(PropertyUtils.getProperty(k, column));
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }
        csvPrinter.println();
    }

    csvPrinter.flush();
}
 
Example 6
Source File: AdminAnalyticsUsersApi.java    From CodeDefenders with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns a CSV file containing the user analytics data.
 * The returned CSV will have a header.
 */
private void doGetCSV(HttpServletResponse response) throws IOException {
    response.setContentType("text/csv");

    List<UserDataDTO> userData = AnalyticsDAO.getAnalyticsUserData();

    String[] columns = new String[]{
        "id",
        "username",
        "gamesPlayed",
        "attackerGamesPlayed",
        "defenderGamesPlayed",
        "attackerScore",
        "defenderScore",
        "mutantsSubmitted",
        "mutantsAlive",
        "mutantsEquivalent",
        "testsSubmitted",
        "mutantsKilled"
    };

    PrintWriter out = response.getWriter();
    CSVPrinter csvPrinter = new CSVPrinter(out, CSVFormat.DEFAULT.withHeader(columns));

    for (UserDataDTO user : userData) {
        for(String column : columns) {
            try {
                csvPrinter.print(PropertyUtils.getProperty(user, column));
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }
        csvPrinter.println();
    }

    csvPrinter.flush();
}
 
Example 7
Source File: CSVUtils.java    From govpay with GNU General Public License v3.0 5 votes vote down vote up
public String toCsv(String ...strings) throws IOException {
	StringWriter writer = new StringWriter();
	CSVPrinter printer = new CSVPrinter(writer, csvFormat);
	printer.printRecord(Arrays.asList(strings));
	printer.flush();
	printer.close();
	
	String string = writer.toString();
	return string;
}
 
Example 8
Source File: FileIndexerPipeline.java    From dataflow-opinion-analysis with Apache License 2.0 4 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) {
	
	ContentIndexSummary summary = c.element();

	if (summary.sentiments == null) 
		return;
	
	try {
	
		StringWriter stringWriter = new StringWriter();
		CSVPrinter csvPrinter = new CSVPrinter(stringWriter,CSVFormat.DEFAULT);
			
		for (int i=0; i < summary.sentiments.length; i++)
		{
			
			ArrayList<String> linefields = new ArrayList<String>();
			
			addField(linefields,"RecordID",summary.doc.collectionItemId);
	
			ArrayList<String> sttags = new ArrayList<>();
			if (summary.sentiments[i].tags != null) 
				for (int j=0; j < summary.sentiments[i].tags.length; j++)
					sttags.add(summary.sentiments[i].tags[j].tag);
			
			addField(linefields,"Tags",sttags.toString()); // will write as [a,b,c]
			
			addField(linefields,"SentimentHash", summary.sentiments[i].sentimentHash);
			addField(linefields,"Text", summary.sentiments[i].text);
			addField(linefields,"LabelledPositions", summary.sentiments[i].labelledPositions);
			addField(linefields,"AnnotatedText", summary.sentiments[i].annotatedText);
			addField(linefields,"AnnotatedHtml", summary.sentiments[i].annotatedHtmlText);
			addField(linefields,"SentimentTotalScore", summary.sentiments[i].sentimentTotalScore);
			addField(linefields,"DominantValence", summary.sentiments[i].dominantValence.ordinal());
			addField(linefields,"StAcceptance", summary.sentiments[i].stAcceptance);
			addField(linefields,"StAnger", summary.sentiments[i].stAnger);
			addField(linefields,"StAnticipation", summary.sentiments[i].stAnticipation);
			addField(linefields,"StAmbiguous", summary.sentiments[i].stAmbiguous);
			addField(linefields,"StDisgust", summary.sentiments[i].stDisgust);
			addField(linefields,"StFear", summary.sentiments[i].stFear);
			addField(linefields,"StGuilt", summary.sentiments[i].stGuilt);
			addField(linefields,"StInterest", summary.sentiments[i].stInterest);
			addField(linefields,"StJoy", summary.sentiments[i].stJoy);
			addField(linefields,"StSadness", summary.sentiments[i].stSadness);
			addField(linefields,"StShame", summary.sentiments[i].stShame);
			addField(linefields,"StSurprise", summary.sentiments[i].stSurprise);
			addField(linefields,"StPositive", summary.sentiments[i].stPositive);
			addField(linefields,"StNegative", summary.sentiments[i].stNegative);
			addField(linefields,"StSentiment", summary.sentiments[i].stSentiment);
			addField(linefields,"StProfane", summary.sentiments[i].stProfane);
			addField(linefields,"StUnsafe", summary.sentiments[i].stUnsafe);
			
			ArrayList<String> signalsarray = new ArrayList<>();
			if (summary.sentiments[i].signals != null) 
				for (int j=0; j < summary.sentiments[i].signals.length; j++)
					signalsarray.add(summary.sentiments[i].signals[j]);
			
			addField(linefields,"Signals",signalsarray.toString());
			
			csvPrinter.printRecord(linefields);
			
			String output = stringWriter.toString().trim(); // need to trim, because printRecord will add the record separator, as will the TextIO.write method at the end of the pipeline
			csvPrinter.flush(); // will also flush the stringWriter
			
			c.output(output);
			
			
		}
		
		csvPrinter.close();
	} catch (IOException e) {
		LOG.warn(e.getMessage());
	}
}
 
Example 9
Source File: Tool.java    From coming with MIT License 4 votes vote down vote up
private void genWeightsCSV() {
        String filePath = Support.getFilePath(Support.DirType.PARAMETER_DIR, option);
        String inputFilePath = filePath + "ParameterVector";
//        System.out.println("inputFilePath" + inputFilePath);
        String[] tmp = Support.getFilePath(Support.DirType.FEATURE_DIR, option).split("/");
        String prefix = tmp[tmp.length - 1];
        String outputFilePath = filePath + prefix + "feature-weights.csv";
//        System.out.println("outputFilePath" + outputFilePath);

        ParameterVector parameterVector = new ParameterVector(option.featureOption);
        parameterVector.load(inputFilePath);

        List<String> header = new ArrayList<>();
        List<String> values = new ArrayList<>();
        header.add("info");
        values.add("weights");
        for (int idx = 0; idx < parameterVector.size(); idx++) {
            FeatureCross featureCross;
            switch (option.featureOption) {
                case ORIGINAL:
                    featureCross = new OriginalFeatureCross(idx);
                    header.add(featureCross.getFeatures().toString());
                    break;
                case EXTENDED:
                    featureCross = new ExtendedFeatureCross(idx);
                    header.add(featureCross.getFeatures().toString());
                    break;
                default:
                    System.out.println("Out of Expectation");
                    break;
            }
            values.add(String.valueOf(parameterVector.get(idx)));
        }

        try {
            BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputFilePath));
            CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(header.toArray(new String[0])));
            // only one line of data
            csvPrinter.printRecord(values);
            csvPrinter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
Example 10
Source File: AdminAnalyticsClassesApi.java    From CodeDefenders with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Returns a CSV file containing the user analytics data.
 * The returned CSV will have a header.
 */
private void doGetCSV(HttpServletResponse response) throws IOException {
    response.setContentType("text/csv");

    List<ClassDataDTO> classData = AnalyticsDAO.getAnalyticsClassData();

    String[] columns = new String[]{
        "id",
        "classname",
        "classalias",
        "nrGames",
        "attackerWins",
        "defenderWins",
        "nrPlayers",
        "testsSubmitted",
        "mutantsSubmitted",
        "mutantsAlive",
        "mutantsEquivalent",
        "ratingsCutMutationDifficultyCount",
        "ratingsCutMutationDifficultySum",
        "ratingsCutTestDifficultyCount",
        "ratingsCutTestDifficultySum",
        "gameEngagingCount",
        "gameEngagingSum"
    };

    String[] ratingNames = new String[] {
        "cutMutationDifficulty",
        "cutTestDifficulty",
        "gameEngaging"
    };

    PrintWriter out = response.getWriter();
    CSVPrinter csvPrinter = new CSVPrinter(out, CSVFormat.DEFAULT.withHeader(columns));

    for (ClassDataDTO clazz : classData) {
        try {
            for(int i = 0; i < 10; i++) {
                csvPrinter.print(PropertyUtils.getProperty(clazz, columns[i]));
            }
            for(String ratingName : ratingNames) {
                ClassDataDTO.ClassRating rating = (ClassDataDTO.ClassRating) PropertyUtils.getProperty(clazz.getRatings(), ratingName);
                csvPrinter.print(rating.getCount());
                csvPrinter.print(rating.getSum());
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        csvPrinter.println();
    }

    csvPrinter.flush();
}
 
Example 11
Source File: AdminKillmapManagementApi.java    From CodeDefenders with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Returns a CSV file containing the user analytics data.
 * The returned CSV will have a header.
 */
private void doGetCSV(HttpServletResponse response, String dataType, String killmapType) throws IOException {
    response.setContentType("text/csv");

    List <? extends KillMapProgress> progresses = getData(dataType, killmapType);
    if (progresses == null) {
        response.setStatus(HttpStatus.SC_BAD_REQUEST);
        return;
    }

    String[] columns;
    if (killmapType.equalsIgnoreCase("class")) {
        columns = new String[]{
            "classId",
            "className",
            "classAlias",
            "nrTests",
            "nrMutants",
            "nrEntries"
        };
    } else {
        columns = new String[]{
            "gameId",
            "gameMode",
            "nrTests",
            "nrMutants",
            "nrEntries"
        };
    }

    PrintWriter out = response.getWriter();
    CSVPrinter csvPrinter = new CSVPrinter(out, CSVFormat.DEFAULT.withHeader(columns));

    for (KillMapProgress progress : progresses) {
        try {
            for (String column : columns) {
                csvPrinter.print(PropertyUtils.getProperty(progress, column));
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        csvPrinter.println();
    }

    csvPrinter.flush();
}