org.apache.commons.csv.CSVPrinter Java Examples
The following examples show how to use
org.apache.commons.csv.CSVPrinter.
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: InformationExtraction2Postgres.java From newsleak with GNU Affero General Public License v3.0 | 8 votes |
private void mappingIdsInMetadata(String mappedMetadataFile) throws Exception { // read mappings file FileInputStream fis = new FileInputStream(this.dataDirectory + File.separator + this.metadataFile + ".id-map"); ObjectInputStream ois = new ObjectInputStream(fis); HashMap<Integer, ArrayList<Integer>> documentIdMapping = (HashMap<Integer, ArrayList<Integer>>) ois .readObject(); ois.close(); // open metadata file, replace ids, write to temporary metadata file BufferedWriter writer = new BufferedWriter(new FileWriter(mappedMetadataFile)); CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.RFC4180); BufferedReader reader = new BufferedReader(new FileReader(this.dataDirectory + File.separator + this.metadataFile)); Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(reader); for (CSVRecord record : records) { Integer tmpDocId = Integer.parseInt(record.get(0)); if (documentIdMapping.containsKey(tmpDocId)) { ArrayList<Integer> mappedIds = documentIdMapping.get(tmpDocId); int nParts = mappedIds.size(); int partCounter = 0; for (Integer newsleakDocId : mappedIds) { String key = StringUtils.capitalize(record.get(1)); String value = record.get(2); if (nParts > 1 && key.equals("Subject")) { partCounter++; value += " (" + partCounter + "/" + nParts + ")"; } ArrayList<String> meta = new ArrayList<String>(); meta.add(newsleakDocId.toString()); meta.add(key); meta.add(value); meta.add(record.get(3)); csvPrinter.printRecord(meta); } } } csvPrinter.close(); reader.close(); }
Example #2
Source File: TestForm.java From zstack with Apache License 2.0 | 8 votes |
private String createCsvContent() { String[][] origin = { {"aBoolean", "anInt", "aFloat", "aDouble", "aLong", "test"}, {"true", "2.3", "3.3", "4.3", "5.3"}, {"True","","","","","a,b,c"}, }; try { ByteArrayOutputStream os = new ByteArrayOutputStream(); OutputStreamWriter out =new OutputStreamWriter(os, Charset.forName("UTF-8")); CSVPrinter printer = new CSVPrinter(out, CSVFormat.RFC4180); printer.printRecords(origin); out.close(); return Base64.getEncoder().encodeToString(os.toByteArray()); } catch (IOException e) { e.printStackTrace(); return null; } }
Example #3
Source File: DataExportController.java From MyBox with Apache License 2.0 | 8 votes |
protected boolean writeInternalCSV(File file, String sql) { try ( Connection conn = DriverManager.getConnection(protocol + dbHome() + login); CSVPrinter printer = new CSVPrinter(new FileWriter(file, Charset.forName("utf-8")), CSVFormat.DEFAULT)) { String filename = file.getAbsolutePath(); conn.setReadOnly(true); writeInternalCSVHeader(printer); int count = 0; try ( ResultSet results = conn.createStatement().executeQuery(sql)) { while (results.next()) { if (cancelled) { updateLogs(message("Cancelled") + " " + filename); return false; } writeInternalCSV(conn, printer, results); count++; if (verboseCheck.isSelected() && (count % 50 == 0)) { updateLogs(message("Exported") + " " + count + ": " + filename); } } } return true; } catch (Exception e) { updateLogs(e.toString()); return false; } }
Example #4
Source File: AgreementUtils.java From webanno with Apache License 2.0 | 8 votes |
private static void configurationSetsWithItemsToCsv(CSVPrinter aOut, AgreementResult<ICodingAnnotationStudy> aAgreement, List<ConfigurationSet> aSets) throws IOException { List<String> headers = new ArrayList<>( asList("Type", "Collection", "Document", "Layer", "Feature", "Position")); headers.addAll(aAgreement.getCasGroupIds()); aOut.printRecord(headers); int i = 0; for (ICodingAnnotationItem item : aAgreement.getStudy().getItems()) { Position pos = aSets.get(i).getPosition(); List<String> values = new ArrayList<>(); values.add(pos.getClass().getSimpleName()); values.add(pos.getCollectionId()); values.add(pos.getDocumentId()); values.add(pos.getType()); values.add(aAgreement.getFeature()); values.add(aSets.get(i).getPosition().toMinimalString()); for (IAnnotationUnit unit : item.getUnits()) { values.add(String.valueOf(unit.getCategory())); } aOut.printRecord(values); i++; } }
Example #5
Source File: KsqlDelimitedSerializer.java From ksql-fork-with-deep-learning-function with Apache License 2.0 | 8 votes |
@Override public byte[] serialize(final String topic, final GenericRow genericRow) { if (genericRow == null) { return null; } try { StringWriter stringWriter = new StringWriter(); CSVPrinter csvPrinter = new CSVPrinter(stringWriter, CSVFormat.DEFAULT); csvPrinter.printRecord(genericRow.getColumns()); String result = stringWriter.toString(); return result.substring(0, result.length() - 2).getBytes(StandardCharsets.UTF_8); } catch (Exception e) { throw new SerializationException("Error serializing CSV message", e); } }
Example #6
Source File: DownloadUtil.java From yanagishima with Apache License 2.0 | 8 votes |
private static void download(HttpServletResponse response, Path resultFilePath, String encode, boolean showHeader, boolean showBOM, char delimiter) { try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), encode))) { try (BufferedReader reader = Files.newBufferedReader(resultFilePath); CSVPrinter printer = new CSVPrinter(writer, CSVFormat.EXCEL.withDelimiter(delimiter).withRecordSeparator(System.getProperty("line.separator")))) { CSVParser parser = CSVFormat.EXCEL.withDelimiter('\t').withNullString("\\N").parse(reader); if (showBOM) { response.getOutputStream().write(BOM); } int rowNumber = 0; for (CSVRecord record : parser) { List<String> columns = new ArrayList<>(); record.forEach(columns::add); if (showHeader || rowNumber > 0) { printer.printRecord(columns); } rowNumber++; } } } catch (IOException e) { throw new RuntimeException(e); } }
Example #7
Source File: RepairEvaluator.java From coming with MIT License | 7 votes |
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 #8
Source File: ClassDupRiskWriter.java From Decca with MIT License | 6 votes |
public void writeRchNum(String outPath, boolean append) { try { CSVPrinter printer = new CSVPrinter(new FileWriter(outPath, append), CSVFormat.DEFAULT); DepJarCgs jarCgs = new DepJarCgs(); for (DupClsJarPair jarPair : getJarPairs().getAllJarPair()) { FourRow fourRow = jarPair.getPairRisk(jarCgs).getFourRow(); printer.printRecord(fourRow.mthdRow); printer.printRecord(fourRow.mthdNameRow); printer.printRecord(fourRow.serviceRow); printer.printRecord(fourRow.serviceNameRow); } printer.close(); } catch (Exception e) { MavenUtil.i().getLog().error("can't write reach class number:", e); } }
Example #9
Source File: ObjectConverter.java From jstarcraft-core with Apache License 2.0 | 6 votes |
@Override public void writeValueTo(CsvWriter context, Type type, Object instance) throws Exception { // TODO 处理null CSVPrinter out = context.getOutputStream(); if (instance == null) { out.print(StringUtility.EMPTY); return; } // 处理对象类型 Class<?> clazz = TypeUtility.getRawType(type, null); ClassDefinition definition = context.getClassDefinition(clazz); int length = definition.getProperties().length; out.print(length); for (PropertyDefinition property : definition.getProperties()) { CsvConverter converter = context.getCsvConverter(property.getSpecification()); Object value = property.getValue(instance); converter.writeValueTo(context, property.getType(), value); } }
Example #10
Source File: InstantConverter.java From jstarcraft-core with Apache License 2.0 | 6 votes |
@Override public void writeValueTo(CsvWriter context, Type type, Object value) throws Exception { CSVPrinter out = context.getOutputStream(); if (value == null) { out.print(StringUtility.EMPTY); return; } // 处理日期类型 if (TypeUtility.isAssignable(type, Date.class)) { value = Date.class.cast(value).getTime(); out.print(value); return; } else { value = Instant.class.cast(value).toEpochMilli(); out.print(value); return; } }
Example #11
Source File: SpannerConverters.java From DataflowTemplates with Apache License 2.0 | 6 votes |
/** * Prints Struct as a CSV String. * * @param struct Spanner Struct. * @return Spanner Struct encoded as a CSV String. */ public String print(Struct struct) { StringWriter stringWriter = new StringWriter(); try { CSVPrinter printer = new CSVPrinter(stringWriter, CSVFormat.DEFAULT.withRecordSeparator("") .withQuoteMode(QuoteMode.ALL_NON_NULL)); LinkedHashMap<String, BiFunction<Struct, String, String>> parsers = Maps.newLinkedHashMap(); parsers.putAll(mapColumnParsers(struct.getType().getStructFields())); List<String> values = parseResultSet(struct, parsers); printer.printRecord(values); } catch (IOException e) { throw new RuntimeException(e); } return stringWriter.toString(); }
Example #12
Source File: CSVGenerator.java From janusgraph-utils with Apache License 2.0 | 6 votes |
/** * Create csv files for a VertexType * @param type a vertex type * @param outputDirectory the output folder to write the csv file */ void writeVertexCSV(VertexTypeBean type, String outputDirectory ){ String csvFile = outputDirectory + "/" + type.name + ".csv"; ArrayList<String> header = new ArrayList<String>(); header.add("node_id"); header.addAll(type.columns.keySet()); int botId = idFactory.getMinId(type.name); int topId = idFactory.getMaxId(type.name); try { CSVPrinter csvFilePrinter = new CSVPrinter(new FileWriter(csvFile), csvFileFormat); csvFilePrinter.printRecord(header); for (int i = botId; i<=topId; i++){ ArrayList<Object> record = new ArrayList<Object>(); record.add(i); record.addAll(generateOneRecord(type.columns)); csvFilePrinter.printRecord(record); } csvFilePrinter.close(); System.out.println("Generated vertex file: "+ csvFile); } catch (Exception e) { throw new RuntimeException(e.toString()); } }
Example #13
Source File: WriteCSVResult.java From nifi with Apache License 2.0 | 6 votes |
public WriteCSVResult(final CSVFormat csvFormat, final RecordSchema recordSchema, final SchemaAccessWriter schemaWriter, final OutputStream out, final String dateFormat, final String timeFormat, final String timestampFormat, final boolean includeHeaderLine, final String charSet) throws IOException { super(out); this.recordSchema = recordSchema; this.schemaWriter = schemaWriter; this.dateFormat = dateFormat; this.timeFormat = timeFormat; this.timestampFormat = timestampFormat; this.includeHeaderLine = includeHeaderLine; final CSVFormat formatWithHeader = csvFormat.withSkipHeaderRecord(true); final OutputStreamWriter streamWriter = new OutputStreamWriter(out, charSet); printer = new CSVPrinter(streamWriter, formatWithHeader); fieldValues = new Object[recordSchema.getFieldCount()]; }
Example #14
Source File: StepMap.java From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 | 6 votes |
public void convertScenarios(File file, List<Scenario> scenarios) { if (scenarios != null && !scenarios.isEmpty()) { try (FileWriter out = new FileWriter(file); CSVPrinter printer = new CSVPrinter(out, CSVFormat.EXCEL.withIgnoreEmptyLines());) { printer.printRecord(HEADERS); for (Scenario scenario : scenarios) { for (TestCase testCase : scenario.getTestCases()) { convertTestCase(testCase, printer); printer.println(); } printer.println(); } } catch (Exception ex) { Logger.getLogger(StepMap.class.getName()).log(Level.SEVERE, "Error while converting", ex); } } }
Example #15
Source File: CollectionConverter.java From jstarcraft-core with Apache License 2.0 | 6 votes |
@Override public void writeValueTo(CsvWriter context, Type type, Collection<Object> value) throws Exception { // TODO 处理null CSVPrinter out = context.getOutputStream(); if (value == null) { out.print(StringUtility.EMPTY); return; } // 兼容UniMi type = TypeUtility.refineType(type, Collection.class); ParameterizedType parameterizedType = ParameterizedType.class.cast(type); Type[] types = parameterizedType.getActualTypeArguments(); Collection<?> collection = Collection.class.cast(value); out.print(collection.size()); Class<?> elementClazz = TypeUtility.getRawType(types[0], null); CsvConverter converter = context.getCsvConverter(Specification.getSpecification(elementClazz)); for (Object element : collection) { converter.writeValueTo(context, types[0], element); } return; }
Example #16
Source File: CSVUtils.java From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 | 6 votes |
public static void saveChanges(GlobalDataModel globalData) { createIfNotExists(globalData.getLocation()); try (FileWriter out = new FileWriter(new File(globalData.getLocation())); CSVPrinter printer = new CSVPrinter(out, CSVFormat.EXCEL.withIgnoreEmptyLines());) { for (String header : globalData.getColumns()) { printer.print(header); } printer.println(); globalData.removeEmptyRecords(); for (List<String> record : globalData.getRecords()) { for (String value : record) { printer.print(value); } printer.println(); } } catch (Exception ex) { Logger.getLogger(CSVUtils.class.getName()).log(Level.SEVERE, "Error while saving", ex); } }
Example #17
Source File: CSVUtils.java From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 | 6 votes |
public static void saveChanges(TestDataModel testData) { createIfNotExists(testData.getLocation()); try (FileWriter out = new FileWriter(new File(testData.getLocation())); CSVPrinter printer = new CSVPrinter(out, CSVFormat.EXCEL.withIgnoreEmptyLines());) { for (String header : testData.getColumns()) { printer.print(header); } printer.println(); testData.removeEmptyRecords(); for (Record record : testData.getRecords()) { for (String value : record) { printer.print(value); } printer.println(); } } catch (Exception ex) { Logger.getLogger(CSVUtils.class.getName()).log(Level.SEVERE, "Error while saving", ex); } }
Example #18
Source File: TestSet.java From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 | 6 votes |
public void save() { if (!isSaved()) { createIfNotExists(); try (FileWriter out = new FileWriter(new File(getLocation())); CSVPrinter printer = new CSVPrinter(out, CSVFormat.EXCEL.withIgnoreEmptyLines());) { printer.printRecord(HEADERS.getValues()); removeEmptySteps(); for (ExecutionStep testStep : testSteps) { printer.printRecord(testStep.exeStepDetails); } setSaved(true); } catch (Exception ex) { Logger.getLogger(TestSet.class.getName()).log(Level.SEVERE, "Error while saving", ex); } } execSettings.save(); }
Example #19
Source File: CsvWriterImpl.java From ganttproject with GNU General Public License v3.0 | 5 votes |
CsvWriterImpl(OutputStream stream, CSVFormat format, boolean addBom) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(stream, Charsets.UTF_8); if (addBom) { writer.write('\ufeff'); } myCsvPrinter = new CSVPrinter(writer, format); }
Example #20
Source File: StepMap.java From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 | 5 votes |
public void convertTestCase(File file, List<TestCase> testCases) { if (testCases != null && !testCases.isEmpty()) { try (FileWriter out = new FileWriter(file); CSVPrinter printer = new CSVPrinter(out, CSVFormat.EXCEL.withIgnoreEmptyLines());) { printer.printRecord(HEADERS); for (TestCase testCase : testCases) { convertTestCase(testCase, printer); printer.println(); } } catch (Exception ex) { Logger.getLogger(StepMap.class.getName()).log(Level.SEVERE, "Error while converting", ex); } } }
Example #21
Source File: TextUtils.java From oryx with Apache License 2.0 | 5 votes |
private static String doJoinDelimited(Iterable<?> elements, CSVFormat format) { StringWriter out = new StringWriter(); try (CSVPrinter printer = new CSVPrinter(out, format)) { for (Object element : elements) { printer.print(element); } printer.flush(); } catch (IOException e) { throw new IllegalStateException(e); } return out.toString(); }
Example #22
Source File: StepMap.java From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 | 5 votes |
public void convertTestCase(File file, TestCase testCase) { if (testCase != null) { try (FileWriter out = new FileWriter(file); CSVPrinter printer = new CSVPrinter(out, CSVFormat.EXCEL.withIgnoreEmptyLines());) { printer.printRecord(HEADERS); convertTestCase(testCase, printer); } catch (Exception ex) { Logger.getLogger(StepMap.class.getName()).log(Level.SEVERE, "Error while converting", ex); } } }
Example #23
Source File: ConnectionsInfoWriter.java From maestro-java with Apache License 2.0 | 5 votes |
public ConnectionsInfoWriter(final File logDir, final String name) throws IOException { File outputFile = new File(logDir, name + ".csv"); writer = Files.newBufferedWriter(Paths.get(outputFile.getPath()), Charset.defaultCharset()); csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT .withHeader("Timestamp", "Container", "Host", "Role", "Dir", "Opened", "Identity", "User", "sasl", "Encrypted", "sslProto", "sslCipher", "Tenant", "Authenticated", "Properties")); }
Example #24
Source File: BooleanConverter.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@Override public void writeValueTo(CsvWriter context, Type type, Object value) throws Exception { // TODO 处理null CSVPrinter out = context.getOutputStream(); if (value == null) { out.print(StringUtility.EMPTY); return; } if (type == AtomicBoolean.class) { value = ((AtomicBoolean) value).get(); } out.print((value.equals(true)) ? TRUE : FALSE); }
Example #25
Source File: StringConverter.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@Override public void writeValueTo(CsvWriter context, Type type, Object value) throws Exception { CSVPrinter out = context.getOutputStream(); if (value == null) { out.print(StringUtility.EMPTY); return; } value = value + StringUtility.SEMICOLON; out.print(value); }
Example #26
Source File: EnumerationConverter.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@Override public void writeValueTo(CsvWriter context, Type type, Object value) throws Exception { CSVPrinter out = context.getOutputStream(); if (value == null) { out.print(StringUtility.EMPTY); return; } Enum<?> enumeration = (Enum<?>) value; int index = enumeration.ordinal(); out.print(index); }
Example #27
Source File: CQL2CSV.java From cqlkit with Apache License 2.0 | 5 votes |
@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 #28
Source File: CSVReaderWriterUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void givenAuthorBookMap_whenWrittenToStream_thenOutputStreamAsExpected() throws IOException { StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withHeader(HEADERS))) { AUTHOR_BOOK_MAP.forEach((author, title) -> { try { printer.printRecord(author, title); } catch (IOException e) { e.printStackTrace(); } }); } assertEquals(EXPECTED_FILESTREAM, sw.toString().trim()); }
Example #29
Source File: CsvFileCreator.java From vividus with Apache License 2.0 | 5 votes |
private void createCsvFile(File directory, CsvFileData csvData, boolean append) throws IOException { File file = new File(directory, csvData.getFileName()); boolean fileExists = file.exists(); OpenOption[] openOptions = append ? new OpenOption[] { StandardOpenOption.CREATE, StandardOpenOption.APPEND } : new OpenOption[] { StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING }; try (Writer writer = new OutputStreamWriter(Files.newOutputStream(file.toPath(), openOptions), StandardCharsets.UTF_8); CSVPrinter printer = append && fileExists ? csvFormat.print(writer) : csvFormat.withHeader(csvData.getHeader()).print(writer)) { printer.printRecords(csvData.getData()); } }
Example #30
Source File: NumberConverter.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@Override public void writeValueTo(CsvWriter context, Type type, Number value) throws Exception { // TODO 处理null CSVPrinter out = context.getOutputStream(); if (value == null) { out.print(StringUtility.EMPTY); return; } out.print(value); }