Jave write csv

60 Jave code examples are found related to " write csv". 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.
Example 1
Source File: CsvWriter.java    From sample-boot-hibernate with MIT License 7 votes vote down vote up
/**
 * CSV書出処理(上書き)を行います。
 * <p>CsvWrite#appendRow 呼び出すタイミングでファイルへ随時書き出しが行われます。
 * @param logic
 */
public void write(final CsvWrite logic) {
    OutputStream out = null;
    try {
        out = fromFile() ? FileUtils.openOutputStream(file) : this.out;
        CsvStream stream = new CsvStream(layout, out);
        logic.execute(stream);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new InvocationException(e);
    } finally {
        if (fromFile()) {
            closeQuietly(out);
        }
    }
}
 
Example 2
Source File: FileUtils.java    From logbook with MIT License 6 votes vote down vote up
/**
 * 報告書をCSVファイルに書き込む
 *
 * @param path ファイル
 * @param header ヘッダー
 * @param body 内容
 * @param applend 追記フラグ
 * @throws IOException
 */
public static void writeCsv(Path path, String[] header, List<String[]> body, boolean applend)
        throws IOException {
    OpenOption[] options;
    if (applend) {
        options = new OpenOption[] { StandardOpenOption.CREATE, StandardOpenOption.APPEND };
    } else {
        options = new OpenOption[] { StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING };
    }
    try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(path, options))) {
        if (!Files.exists(path) || (Files.size(path) <= 0)) {
            out.write(StringUtils.join(header, ',').getBytes(AppConstants.CHARSET));
            out.write("\r\n".getBytes(AppConstants.CHARSET));
        }
        for (String[] colums : body) {
            out.write(StringUtils.join(colums, ',').getBytes(AppConstants.CHARSET));
            out.write("\r\n".getBytes(AppConstants.CHARSET));
        }
    }
}
 
Example 3
Source File: CsvTools.java    From pt2matsim with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Writes a map within a map to a file, each value V is on a line with its corresponding
 * E and K values.
 */
public static <K, E, V> void writeNestedMapToFile(Object[] header, Map<K, Map<E, V>> map, String filename, char separator) throws IOException {
	List<String> csvLines = new LinkedList<>();

	if(header != null) {
		String headerLine = header[0].toString() + separator + header[1].toString() + separator + header[2].toString();
		csvLines.add(headerLine);
	}

	for(Map.Entry<K, Map<E, V>> entry : map.entrySet()) {
		for(Map.Entry<E, V> f : entry.getValue().entrySet()) {
			String newLine = "";
			newLine += entry.getKey().toString() + separator;
			newLine += f.getKey().toString() + separator;
			newLine += f.getValue().toString();
			csvLines.add(newLine);
		}
	}
	writeToFile(csvLines, filename);
}
 
Example 4
Source File: CsvFormatter.java    From yarg with Apache License 2.0 6 votes vote down vote up
protected void writeCsvDocument(BandData rootBand, OutputStream outputStream) {
    try {
        List<BandData> actualData = getActualData(rootBand);
        CSVWriter writer = new CSVWriter(new OutputStreamWriter(outputStream), separator, CSVWriter.DEFAULT_QUOTE_CHARACTER);

        writer.writeNext(header);

        for (BandData row : actualData) {
            String[] entries = new String[parametersToInsert.size()];
            for (int i = 0; i < parametersToInsert.size(); i++) {
                String parameterName = parametersToInsert.get(i);
                String fullParameterName = row.getName() + "." + parameterName;
                entries[i] = formatValue(row.getData().get(parameterName), parameterName, fullParameterName);
            }
            writer.writeNext(entries);
        }

        writer.close();
    } catch (IOException e) {
        throw new ReportFormattingException("Error while writing a csv document", e);
    }
}
 
Example 5
Source File: ExportCsv.java    From incubator-iotdb with Apache License 2.0 6 votes vote down vote up
private static int writeResultSet(ResultSet rs, BufferedWriter bw, int count)
    throws SQLException, IOException {
  int line = 0;
  long timestamp = System.currentTimeMillis();
  while (rs.next()) {
    if (rs.getString(1) == null ||
        "null".equalsIgnoreCase(rs.getString(1))) {
      bw.write(",");
    } else {
      writeTime(rs, bw);
      writeValue(rs, count, bw);
    }
    line++;
    if (line % EXPORT_PER_LINE_COUNT == 0) {
      long tmp = System.currentTimeMillis();
      System.out.println(
          String.format("%d lines have been exported, it takes %dms", line, (tmp - timestamp)));
      timestamp = tmp;
    }
  }
  return line;
}
 
Example 6
Source File: ExportCsv.java    From incubator-iotdb with Apache License 2.0 6 votes vote down vote up
private static void writeTime(ResultSet rs, BufferedWriter bw) throws SQLException, IOException {
  ZonedDateTime dateTime;
  switch (timeFormat) {
    case "default":
      long timestamp = rs.getLong(1);
      String str = AbstractCli
          .parseLongToDateWithPrecision(DateTimeFormatter.ISO_OFFSET_DATE_TIME, timestamp, zoneId,
              TIMESTAMP_PRECISION);
      bw.write(str + ",");
      break;
    case "timestamp":
    case "long":
    case "nubmer":
      bw.write(rs.getLong(1) + ",");
      break;
    default:
      dateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(rs.getLong(1)),
          zoneId);
      bw.write(dateTime.format(DateTimeFormatter.ofPattern(timeFormat)) + ",");
      break;
  }
}
 
Example 7
Source File: CsvWriter.java    From sample-boot-hibernate with MIT License 6 votes vote down vote up
/**
 * CSV書出処理(追記)を行います。
 * <p>CsvWrite#appendRow 呼び出すタイミングでファイルへ随時書き出しが行われます。
 * <p>ファイル出力時のみ利用可能です。
 * @param logic
 */
public void writeAppend(final CsvWrite logic) {
    if (!fromFile())
        throw new UnsupportedOperationException("CSV書出処理の追記はファイル出力時のみサポートされます");
    FileOutputStream out = null;
    try {
        out = FileUtils.openOutputStream(file, true);
        CsvStream stream = new CsvStream(layout, out);
        logic.execute(stream);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new InvocationException(e);
    } finally {
        closeQuietly(out);
    }
}
 
Example 8
Source File: ObjectDataExportServiceImpl.java    From axelor-open-suite with GNU Affero General Public License v3.0 6 votes vote down vote up
private MetaFile writeCSV(Map<String, List<String[]>> data) throws IOException {

    File zipFile = MetaFiles.createTempFile("Data", ".zip").toFile();
    try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(zipFile))) {

      for (String model : data.keySet()) {
        File modelFile = MetaFiles.createTempFile(model, ".csv").toFile();
        CSVWriter writer = new CSVWriter(new FileWriter(modelFile), ';');
        writer.writeAll(data.get(model));
        writer.close();
        zout.putNextEntry(new ZipEntry(model + ".csv"));
        zout.write(IOUtils.toByteArray(new FileInputStream(modelFile)));
        zout.closeEntry();
      }
      zout.close();
    }
    return metaFiles.upload(zipFile);
  }
 
Example 9
Source File: SmartCSVWriter.java    From constellation with Apache License 2.0 6 votes vote down vote up
/**
 * Output a new row.
 *
 * @param row the fields of the new row.
 * @throws IOException if the underlying Writer throws an IOException.
 */
public void writeNext(String[] row) throws IOException {

    boolean needsDelimiter = false;
    for (String field : row) {

        if (needsDelimiter) {
            out.write(separator);
        } else {
            needsDelimiter = true;
        }

        if (field != null) {
            if (escapeAlways || field.indexOf('"') >= 0 || field.indexOf(separator) >= 0 || field.indexOf('\n') >= 0 || field.indexOf('\r') >= 0) {
                out.write('"');
                out.write(field.replaceAll("\"", "\"\""));
                out.write('"');
            } else {
                out.write(field);
            }
        }
    }

    out.write('\n');
}
 
Example 10
Source File: CSVFile.java    From ECG-Viewer with GNU General Public License v2.0 6 votes vote down vote up
/**
 * write - writes data to the file
 *
 * @param data The data to write. 
 * 		Array describes (in order of outermost to innermost):
 *			1. channel number
 *			2. x (0) or y (1)
 *			3. sample number
 */
public void write(ECGDataSet[] data, int offset) 
		throws IOException {
	String s = data.length + " leads sampled @" + (data[0].getAt(1)[0]-data[0].getAt(0)[0]) + "ms";
	out.write(s, 0, s.length());
	for(int i = 0; i < data.length; i++) {
		out.write(",\t\t", 0, 3);
		out.write("" + (i+offset), 0, new Integer(i+offset).toString().length());
	}
	out.write("\n", 0, 1);

	for(int i = 0; i < data[0].size(); i++) {
		s = String.format("%f", data[0].getAt(i)[0]);
		out.write("" + s, 0, s.length());
		for(int j = 0; j < data.length; j++) {
			s = String.format("%f", data[j].getAt(i)[1]);
			out.write(",\t" + s, 0, s.length() + 2);
		}
		out.write("\n", 0, 1);
	}

	out.flush();
	out.close();
}
 
Example 11
Source File: CSVFile.java    From ECG-Viewer with GNU General Public License v2.0 6 votes vote down vote up
/**
 * writeSubset - writes part of the data to a file
 *
 * @param data the data to write. See write() for more detail
 * @param start the index to start at
 * @param end the index to end at
 */
public void writeSubset(ECGDataSet[] data, int start, int end, int offset)
		throws IOException {
	String s = data.length + " leads sampled @" + (data[0].getAt(1)[0]-data[0].getAt(0)[0]) + "ms";
	out.write(s, 0, s.length());
	for(int i = 0; i < data.length; i++) {
		out.write(",\t\t", 0, 3);
		out.write("" + (i+offset), 0, new Integer(i+offset).toString().length());
	}
	out.write("\n", 0, 1);

	for(int i = start; i < end; i++) {
		s = String.format("%f", data[0].getAt(i)[0]);
		out.write("" + s, 0, s.length());
		for(int j = 0; j < data.length; j++) {
			s = String.format("%f", data[j].getAt(i)[1]);
			out.write(",\t" + s, 0, s.length() + 2);
		}
		out.write("\n", 0, 1);
	}

	out.flush();
	out.close();
}
 
Example 12
Source File: TestCsvFileSystemFormatFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
private static void writeCsvToStream(
		DataType[] types,
		RowData rowData,
		OutputStream stream) throws IOException {
	LogicalType[] fieldTypes = Arrays.stream(types)
			.map(DataType::getLogicalType)
			.toArray(LogicalType[]::new);
	DataFormatConverters.DataFormatConverter converter = DataFormatConverters.getConverterForDataType(
			TypeConversions.fromLogicalToDataType(RowType.of(fieldTypes)));

	Row row = (Row) converter.toExternal(rowData);
	StringBuilder builder = new StringBuilder();
	Object o;
	for (int i = 0; i < row.getArity(); i++) {
		if (i > 0) {
			builder.append(DEFAULT_FIELD_DELIMITER);
		}
		if ((o = row.getField(i)) != null) {
			builder.append(o);
		}
	}
	String str = builder.toString();
	stream.write(str.getBytes(StandardCharsets.UTF_8));
	stream.write(DEFAULT_LINE_DELIMITER.getBytes(StandardCharsets.UTF_8));
}
 
Example 13
Source File: CsvConfigFileWriter.java    From oodt with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
protected File writeCsvFile(String file, List<String> header,
      List<List<String>> rows, String delim) throws IOException {
   File outputFile = new File(file);
   PrintStream ps = null;
   try {
      ps = new PrintStream(new FileOutputStream(outputFile));
      ps.println(Joiner.on(delim).join(header));
      for (List<String> row : rows) {
         ps.println(Joiner.on(delim).join(row));
      }
      return outputFile;
   } catch (Exception e) {
      throw new IOException("Failed to write CSV file '" + file + "' : "
            + e.getMessage(), e);
   } finally {
      try {
         ps.close();
      } catch (Exception ignore) {
      }
   }
}
 
Example 14
Source File: MiscellaneousCommands.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/***
 * Writes to a TabularResultData and also appends a CSV string to a String builder
 * @param metricsTable
 * @param type
 * @param metricName
 * @param metricValue
 * @param csvBuilder
 */
private void writeToTableAndCsv(TabularResultData metricsTable, String type,
    String metricName, String metricValue, StringBuilder csvBuilder) {
  metricsTable.accumulate(CliStrings.SHOW_METRICS__TYPE__HEADER, type);
  metricsTable.accumulate(CliStrings.SHOW_METRICS__METRIC__HEADER, metricName);
  metricsTable.accumulate(CliStrings.SHOW_METRICS__VALUE__HEADER, metricValue);

  if (csvBuilder != null) {
    csvBuilder.append(type);
    csvBuilder.append(',');
    csvBuilder.append(metricName);
    csvBuilder.append(',');
    csvBuilder.append(metricValue);
    csvBuilder.append('\n');
  }
}
 
Example 15
Source File: CsvWriter.java    From fastnfitness with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void writeComment(String commentText) throws IOException {
	checkClosed();

	checkInit();

	outputStream.write(userSettings.Comment);

	outputStream.write(commentText);

	if (useCustomRecordDelimiter) {
		outputStream.write(userSettings.RecordDelimiter);
	} else {
		outputStream.write(systemRecordDelimiter);
	}
	
	firstColumn = true;
}
 
Example 16
Source File: JacksonCSVSplitterTest.java    From java-client-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testDocumentWriteOperation() throws Exception {
    Stream<StringHandle> strStream = Stream.of(new StringHandle("first"), new StringHandle("second"));
   
    Stream<DocumentWriteOperation> documentStream =  DocumentWriteOperation.from(
            strStream, DocumentWriteOperation.uriMaker("/sample/directory/%s.json")
            );
    
    assertNotNull(documentStream);
    String[] stringHandleValues = {"first", "second"};
    Iterator<DocumentWriteOperation> itr = documentStream.iterator();
    for(int i=0; itr.hasNext(); i++) {
        DocumentWriteOperation docOp = itr.next();
        assertNotNull(docOp.getUri());
        assertNotNull(docOp.getContent());
        assertTrue(docOp.getContent().toString().equals(stringHandleValues[i]));
    }
}
 
Example 17
Source File: CSVWriterTest.java    From projectforge-webapp with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testWriteCSV() throws Exception
{
  Date date = createDate(1970, Calendar.NOVEMBER, 21, 14, 17, 57, 742);
  StringWriter writer = new StringWriter();
  CSVWriter csv = new CSVWriter(writer);
  csv.write("Hallo");
  csv.write("Hal\"lo");
  csv.write((String) null);
  csv.write((Date) null);
  csv.write(date);
  csv.write(42);
  csv.writeEndOfLine();
  csv.flush();
  assertEquals("\"Hallo\";\"Hal\"\"lo\";;;\"1970-11-21 13:17:57.742\";42\n", writer.toString());
  csv.write("\"");
  csv.writeEndOfLine();
  csv.flush();
  assertEquals("\"Hallo\";\"Hal\"\"lo\";;;\"1970-11-21 13:17:57.742\";42\n\"\"\"\"\n", writer.getBuffer().toString());
}
 
Example 18
Source File: CSVParserTest.java    From projectforge-webapp with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testWriteAndParseCSV() throws Exception
{
  String[][] fields = { { "1.1", "1.2", "1.3"}, { "2.1", "2.2", "2.3"}, { "3.1", "3.2", "3.3"}};
  StringWriter writer = new StringWriter();
  CSVWriter csvWriter = new CSVWriter(writer);
  csvWriter.writeLine(fields[0]);
  csvWriter.writeLine(fields[1]);
  csvWriter.writeLine(fields[2]);
  StringReader in = new StringReader(writer.toString());
  CSVParser parser = new CSVParser(in);
  assertLine(parser.parseLine(), "1.1", "1.2", "1.3");
  assertLine(parser.parseLine(), "2.1", "2.2", "2.3");
  assertLine(parser.parseLine(), "3.1", "3.2", "3.3");
  assertNull(parser.parseLine());
}
 
Example 19
Source File: CSVWriterTest.java    From data-prep with Apache License 2.0 6 votes vote down vote up
@Test
public void write_shouldWriteIso885_1() throws Exception {
    // given
    final DataSetRow row = buildSimpleRow();
    row.set("0002", NON_ASCII_TEST_CHARS);

    Map<String, String> parameters = new HashMap<>();
    parameters.put(ExportFormat.PREFIX + CSVFormat.ParametersCSV.ENCODING, StandardCharsets.ISO_8859_1.name());

    // when
    ByteArrayOutputStream out = writeCsv(parameters, row.getRowMetadata(), singletonList(row));

    // then
    assertThat(out.toByteArray())
            .isEqualTo(("\"id\";\"firstname\"\n\"64a5456ac148b64524ef165\";\"" + NON_ASCII_TEST_CHARS + "\"\n")
                    .getBytes(ISO_8859_1));
    assertThat(out.toByteArray())
            .isNotEqualTo(("\"id\";\"firstname\"\n\"64a5456ac148b64524ef165\";\"" + NON_ASCII_TEST_CHARS + "\"\n")
                    .getBytes(UTF_8));
}
 
Example 20
Source File: CSVWriterTest.java    From data-prep with Apache License 2.0 6 votes vote down vote up
@Test
public void write_shouldDefaultToUtf8() throws Exception {
    // given
    final DataSetRow row = buildSimpleRow();
    row.set("0002", NON_ASCII_TEST_CHARS);

    Map<String, String> parameters = new HashMap<>();
    String invalidCharsetName = "ISO-8859-560";
    parameters.put(ExportFormat.PREFIX + CSVFormat.ParametersCSV.ENCODING, invalidCharsetName);

    // when
    ByteArrayOutputStream out = writeCsv(parameters, row.getRowMetadata(), singletonList(row));

    // then
    assertThat(out.toByteArray())
            .isEqualTo(("\"id\";\"firstname\"\n\"64a5456ac148b64524ef165\";\"" + NON_ASCII_TEST_CHARS + "\"\n")
                    .getBytes(UTF_8));
}
 
Example 21
Source File: CsvExport.java    From financisto with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void writeBody(BufferedWriter bw) throws IOException {
    Csv.Writer w = new Csv.Writer(bw).delimiter(options.fieldSeparator);
    try {
        accountsMap = db.getAllAccountsMap();
        categoriesMap = db.getAllCategoriesMap();
        payeeMap = db.getAllPayeeByIdMap();
        projectMap = db.getAllProjectsByIdMap(true);
        locationMap = db.getAllLocationsByIdMap(false);
        try (Cursor c = db.getBlotter(options.filter)) {
            while (c.moveToNext()) {
                Transaction t = Transaction.fromBlotterCursor(c);
                writeLine(w, t);
            }
        }
    } finally {
        w.close();
    }
}
 
Example 22
Source File: FastqStatsRunner.java    From hmftools with GNU General Public License v3.0 6 votes vote down vote up
private static void writeOutputToCSV(@NotNull final String flowcellName, @NotNull final FastqTracker tracker,
        @NotNull final String csvOutPath) throws IOException {
    final BufferedWriter writer = new BufferedWriter(new FileWriter(csvOutPath, false));
    writer.write("Flowcell," + flowcellName + "," + tracker.flowcell().yield() + "," + tracker.flowcell().q30Percentage() + "\n");

    for (final String laneName : tracker.lanes().keySet()) {
        final FastqData lane = tracker.lane(laneName);
        writer.write("Lane," + laneName + "," + lane.yield() + "," + lane.q30Percentage() + "\n");
    }
    for (final String sampleName : tracker.samples().keySet()) {
        final FastqData sample = tracker.sample(sampleName);
        writer.write("Sample," + sampleName + "," + sample.yield() + "," + sample.q30Percentage() + "\n");
    }
    for (final String sampleName : tracker.samples().keySet()) {
        for (final String laneName : tracker.samples().get(sampleName).keySet()) {
            final FastqData sampleLaneData = tracker.samples().get(sampleName).get(laneName);
            writer.write(sampleName + "," + laneName + "," + sampleLaneData.yield() + "," + sampleLaneData.q30Percentage() + "\n");
        }
    }
    writer.close();
    LOGGER.info("Written fastq qc data to {}", csvOutPath);
}
 
Example 23
Source File: Executor.java    From LicenseScout with Apache License 2.0 6 votes vote down vote up
/**
 * @param archives
 * @throws LicenseScoutExecutionException
 */
private void writeArchiveCsvSkeletonFile(final List<Archive> archives) throws LicenseScoutExecutionException {
    if (getExecutionParameters().isWriteArchiveCsvSkeleton()) {
        if (getExecutionParameters().getArchiveCsvSkeletonFile() != null) {
            getLog().info("writing archive CSV skeleton file to: "
                    + getExecutionParameters().getArchiveCsvSkeletonFile());
            try {
                LicenseCheckedList.writeCsvSkeletonFile(getExecutionParameters().getArchiveCsvSkeletonFile(),
                        archives);
            } catch (IOException e) {
                throw new LicenseScoutExecutionException("cannot write archive CSV skeleton file", e);
            }
        } else {
            getLog().info("Not writing archive CSV skeleton file (not configured)");
        }
    } else {
        getLog().info("Not writing archive CSV skeleton file (not configured)");
    }
}
 
Example 24
Source File: CSVExportTaxonomy.java    From megan-ce with GNU General Public License v3.0 6 votes vote down vote up
/**
 * write readname and matches to taxa
 *
 * @param separator
 * @param readName
 * @param readBlock
 * @param w
 * @return number of matches in output
 * @throws IOException
 */
private static int writeMatches(char separator, String readName, IReadBlock readBlock, Writer w) throws IOException {

    int countMatches = 0;
    if (readBlock.getNumberOfAvailableMatchBlocks() == 0)
        w.write(String.format("%s%c\n", readName, separator));
    else {
        w.write(readName);
        for (IMatchBlock matchBlock : readBlock.getMatchBlocks()) {
            w.write(String.format("%c%d%c%.2f", separator, matchBlock.getTaxonId(), separator, matchBlock.getBitScore()));
            countMatches++;
        }
        w.write("\n");

    }
    return countMatches;
}
 
Example 25
Source File: CsvWriter.java    From oxygen with Apache License 2.0 6 votes vote down vote up
private void writeElement(int index, String element, Appendable appendable, boolean applyQuotes) {
  if (index != 0) {
    append(appendable, separator);
  }
  if (element == null) {
    return;
  }

  boolean hasSpecialChar = hasSpecialChar(element);
  appendQuote(applyQuotes, hasSpecialChar, appendable);
  if (hasSpecialChar) {
    processLine(element, appendable);
  } else {
    append(appendable, element);
  }
  appendQuote(applyQuotes, hasSpecialChar, appendable);
}
 
Example 26
Source File: InventoryReportLineWriter.java    From s3-inventory-usage-examples with Apache License 2.0 6 votes vote down vote up
/**
 * Write a new inventory report to S3 and returns a locator which includes this inventory report's information
 * @return Locator which includes the information of this new report
 * @throws IOException thrown when GZIPOutputStream not created successfully or csvMapper.write() fails
 */
public InventoryManifest.Locator writeCsvFile(List<InventoryReportLine> inventoryReportLine) throws IOException{
    CsvMapper csvMapper = new CsvMapper();
    csvMapper.enable(JsonGenerator.Feature.IGNORE_UNKNOWN);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
    csvMapper.writer(schema).writeValues(gzipOutputStream).writeAll(inventoryReportLine).close();
    byte[] zipByteArray = byteArrayOutputStream.toByteArray();

    InputStream zipInputStream = new ByteArrayInputStream(zipByteArray);
    ObjectMetadata metaData = new ObjectMetadata();
    metaData.setContentLength(zipByteArray.length);
    PutObjectRequest request = new PutObjectRequest(bucketName, outputInventoryReportKey, zipInputStream, metaData);
    s3Client.putObject(request);

    return this.buildLocator(zipByteArray);
}
 
Example 27
Source File: BenchmarkResultsRecorder.java    From twister2 with Apache License 2.0 6 votes vote down vote up
public void writeToCSV() {
  if (!this.accept) {
    return;
  }
  try {
    //go through the existing columns of csv and determine the order
    List<String> existingHeaderOrder = new ArrayList<>();
    String headerLine = "";
    if (csvFile.exists()) {
      BufferedReader bufferedReader = new BufferedReader(new FileReader(csvFile));
      headerLine = bufferedReader.readLine();
      bufferedReader.close();
      if (headerLine != null) {
        existingHeaderOrder.addAll(Arrays.asList(headerLine.split(",")));
      }
    }
    this.writeInOrder(existingHeaderOrder, headerLine);
    LOG.info("Wrote results to " + this.csvFile.getAbsolutePath());
  } catch (IOException ioex) {
    LOG.log(Level.SEVERE, "Couldn't write results to the file", ioex);
    throw new RuntimeException("Error in writing results to the file", ioex);
  }
}
 
Example 28
Source File: SimpleCSVWriterWrapperWithHeaderUnitTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test (groups = "bucket")
public void testWriteToBucketPathEquivalentToLocalPath() throws IOException {
    Path bucketPath = IOUtils.getPath(BucketUtils.getTempFilePath(
            getGCPTestStaging() + "testWriteToBucketPathEquivalentToLocalPath", ".tsv"));
    Path localPath = IOUtils.getPath(createTempFile("testWriteToBucketPathEquivalentToLocalPath", ".tsv").getPath());

    try (SimpleXSVWriter bucketWriter = new SimpleXSVWriter(bucketPath, '\t');
         SimpleXSVWriter localWriter = new SimpleXSVWriter(localPath, '\t')) {

        String[] header = new String[]{"a", "b", "c"};
        bucketWriter.setHeaderLine(Arrays.asList(header));
        localWriter.setHeaderLine(Arrays.asList(header));

        for (int i = 0; i < 100; i++) {
            SimpleXSVWriter.LineBuilder bucketLine = bucketWriter.getNewLineBuilder();
            SimpleXSVWriter.LineBuilder localLine = localWriter.getNewLineBuilder();
            int finalI = i;
            Arrays.stream(header).forEach(column -> {
                bucketLine.setColumn(column, Integer.toString(finalI));
                localLine.setColumn(column, Integer.toString(finalI));
            });
        }
    }
    IntegrationTestSpec.assertEqualTextFiles(bucketPath, localPath, null);
}
 
Example 29
Source File: ReadWriteCSV.java    From AIDR with GNU Affero General Public License v3.0 6 votes vote down vote up
private static CellProcessor[] getProcessors4ClassifiedTweetIDSCCSV() {

		final CellProcessor[] processors = new CellProcessor[]{
				//new UniqueHashCode(), // tweetID (must be unique)
				new NotNull(),	// tweetID (must be unique): sometimes CAN be null!
				new NotNull(), 	// crisis name
				new Optional(),	// attribute name
				new Optional(), // attribute code
				new Optional(), // label name
				new Optional(), // label description
				new Optional(),	// label code
				new Optional(), // confidence
				new Optional(), // humanLabeled
		};
		return processors;
	}
 
Example 30
Source File: StringWritableCsv.java    From pgadba with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Write a sequence of {@link StringWritableCsv}s to a StringBuffer. Null {@link StringWritable}s are not printed, but separator
 * is still used. Separator is a comma (',')
 *
 * @param sb The sb to write to
 * @param values Zero or more attribute-value pairs to write
 * @return The same sb, with data filled in (if any)
 * @throws IllegalArgumentException If sb is null
 */
public static StringBuffer writeTo(StringBuffer sb, StringWritable... values) throws IllegalArgumentException {
  checkNotNull(sb, "sb");
  if (null == values || values.length == 0) {
    return sb;
  }

  writeStringWritableToStringBuffer(values[0], sb);
  int i = 1;
  while (i < values.length) {
    sb.append(',');
    writeStringWritableToStringBuffer(values[i], sb);
    i++;
  }

  return sb;
}
 
Example 31
Source File: CsvClientTest.java    From CSVeed with Apache License 2.0 6 votes vote down vote up
private void writeRows(String lineTerminators) throws IOException {
    try (StringWriter writer = new StringWriter()) {
        CsvClient<StringWriter> csvClient = new CsvClientImpl<StringWriter>(writer)
            .setUseHeader(false)
            .setEndOfLine(lineTerminators.toCharArray());
        csvClient.writeHeader(new String[] {
            "h1", "h2", "h3"
        } );
        csvClient.writeRows(new String[][] {
            { "l1c1", "l1c2", "l1c3" },
            { "l2c1", "l2c2", "l2c3" },
            { "l3c1", "l3c2", "l3c3" }
        } );
    
        assertEquals(
            "\"h1\";\"h2\";\"h3\"" + lineTerminators +
                    "\"l1c1\";\"l1c2\";\"l1c3\"" + lineTerminators +
                    "\"l2c1\";\"l2c2\";\"l2c3\"" + lineTerminators +
                    "\"l3c1\";\"l3c2\";\"l3c3\"" + lineTerminators,
            writer.getBuffer().toString());
    }
}
 
Example 32
Source File: CsvClientTest.java    From CSVeed with Apache License 2.0 6 votes vote down vote up
@Test
public void writeBeansBasedOnClass() throws IOException {
    try (StringWriter writer = new StringWriter()) {
        List<BeanWithMultipleStrings> beans = new ArrayList<>();
        beans.add(createBean("row 1, cell 3", "row 1, cell 2", "row 1, cell 1"));
        beans.add(createBean("row 2, cell 3", "row 2, cell 2", "row 2, cell 1"));
        beans.add(createBean("row 3, cell 3", "row 3, cell 2", "row 3, cell 1"));
        CsvClient<BeanWithMultipleStrings> client = new CsvClientImpl<>(
                writer, BeanWithMultipleStrings.class
        );
        client.writeBeans(beans);
    
        assertEquals(
            "\"gamma\";\"beta\";\"alpha\"\r\n"+
            "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\r\n"+
            "\"row 2, cell 1\";\"row 2, cell 2\";\"row 2, cell 3\"\r\n"+
            "\"row 3, cell 1\";\"row 3, cell 2\";\"row 3, cell 3\"\r\n",
            writer.getBuffer().toString());
    }
}
 
Example 33
Source File: CsvClientTest.java    From CSVeed with Apache License 2.0 6 votes vote down vote up
@Test
public void writeBeansBasedOnInstructions() throws IOException {
    try (StringWriter writer = new StringWriter()) {
        List<BeanWithMultipleStrings> beans = new ArrayList<>();
        beans.add(createBean("row 1, cell 3", "row 1, cell 2", "row 1, cell 1"));
        beans.add(createBean("row 2, cell 3", "row 2, cell 2", "row 2, cell 1"));
        beans.add(createBean("row 3, cell 3", "row 3, cell 2", "row 3, cell 1"));
        CsvClient<BeanWithMultipleStrings> client = new CsvClientImpl<>(
            writer, new BeanInstructionsImpl(BeanWithMultipleStrings.class)
                .mapColumnNameToProperty("alpha", "alpha")
                .mapColumnNameToProperty("beta", "beta")
                .ignoreProperty("gamma")
                );
        client.writeBeans(beans);
    
        assertEquals(
            "\"beta\";\"alpha\"\r\n"+
            "\"row 1, cell 2\";\"row 1, cell 3\"\r\n"+
            "\"row 2, cell 2\";\"row 2, cell 3\"\r\n"+
            "\"row 3, cell 2\";\"row 3, cell 3\"\r\n",
            writer.getBuffer().toString());
    }
}
 
Example 34
Source File: DelimiterWriterTest.java    From flatpack with Apache License 2.0 6 votes vote down vote up
public void testWriteCsvWithMappingFile() throws Exception {
    final InputStream mapping = this.getClass().getClassLoader().getResourceAsStream("DelimitedWithHeader.pzmap.xml");
    final Reader mappingReader = new InputStreamReader(mapping);
    final StringWriter out = new StringWriter();

    final Writer writer = new DelimiterWriterFactory(mappingReader, ';', '"').createWriter(out);
    writer.addRecordEntry("LASTNAME", "ANAME");
    writer.addRecordEntry("FIRSTNAME", "JOHN");
    writer.addRecordEntry("ZIP", "44035");
    writer.addRecordEntry("CITY", "ELYRIA");
    writer.addRecordEntry("STATE", "OH");
    writer.addRecordEntry("ADDRESS", "1234 CIRCLE CT");
    writer.nextRecord();
    writer.flush();

    final String expected = this.joinLines("FIRSTNAME;LASTNAME;ADDRESS;CITY;STATE;ZIP", "JOHN;ANAME;1234 CIRCLE CT;ELYRIA;OH;44035");

    Assert.assertEquals(expected, out.toString());
}
 
Example 35
Source File: DelimiterWriterTest.java    From flatpack with Apache License 2.0 6 votes vote down vote up
public void testWriteCsvWithMissingColumns() throws Exception {
    final InputStream mapping = this.getClass().getClassLoader().getResourceAsStream("DelimitedWithHeader.pzmap.xml");
    final InputStreamReader mappingReader = new InputStreamReader(mapping);
    final StringWriter out = new StringWriter();

    final Writer writer = new DelimiterWriterFactory(mappingReader, ';', '"').createWriter(out);
    // note that we do not provide values for FIRSTNAME and ADDRESS
    writer.addRecordEntry("LASTNAME", "ANAME");
    writer.addRecordEntry("ZIP", "44035");
    writer.addRecordEntry("CITY", "ELYRIA");
    writer.addRecordEntry("STATE", "OH");
    writer.nextRecord();
    writer.flush();

    final String expected = this.joinLines("FIRSTNAME;LASTNAME;ADDRESS;CITY;STATE;ZIP", ";ANAME;;ELYRIA;OH;44035");

    Assert.assertEquals(expected, out.toString());
}
 
Example 36
Source File: SampleImporterFromSHRIMPLegacyCSVFile_SIRCOMBE.java    From ET_Redux with Apache License 2.0 6 votes vote down vote up
/**
 * 
 */
public static void writeAndOpenCSVFileOfLegacyDataSampleFieldNames() {
    String fieldNames = TemplatesForCsvImport.IDTIMSLegacyDataSampleFieldNames_MIT;
    File CSVFile = new File("IDTIMSLegacySampleFieldNamesTemplate" + ".csv");
    CSVFile.delete();
    PrintWriter outputWriter = null;
    try {
        outputWriter = new PrintWriter(new FileWriter(CSVFile));

        outputWriter.println("LEGACY ID-TIMS (MIT) DATA SAMPLE FIELD NAMES FOR IMPORT INTO ET_Redux\n");
        outputWriter.println("FractionName, IsotopicRatios, , , , , , , , , Composition, , , , , , , IsotopicDates (Ma)");
        outputWriter.println(fieldNames);

        outputWriter.close();

    } catch (IOException iOException) {
    }

    try {
        BrowserControl.displayURL(CSVFile.getCanonicalPath());
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}
 
Example 37
Source File: DataExportController.java    From MyBox with Apache License 2.0 6 votes vote down vote up
protected boolean writeExternalCSV(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);
        writeExternalCSVHeader(printer);
        int count = 0;
        try ( ResultSet results = conn.createStatement().executeQuery(sql)) {
            while (results.next()) {
                if (cancelled) {
                    updateLogs(message("Cancelled") + " " + filename);
                    return false;
                }
                writeExternalCSV(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 38
Source File: ProduceCsvFiles.java    From collect-earth with MIT License 6 votes vote down vote up
private File writeLinesToFile(String firstLine, List<String> lines) throws IOException {
	File randomizedFile = File.createTempFile("randomizeLines", "txt");

	try ( 
		BufferedOutputStream writer = new BufferedOutputStream( new FileOutputStream(randomizedFile ) )
	){
		Charset utfCharset = Charset.forName("UTF-8");

		// Write the possible header
		writer.write( (firstLine + "\r\n").getBytes( utfCharset));

		if( lines != null ){
			// Then append the next lines
			for(String line: lines) {
				line = line + "\r\n";
				writer.write( line.getBytes( utfCharset));
			}
		}
	}

	return randomizedFile;
}
 
Example 39
Source File: QuerySigrid.java    From collect-earth with MIT License 6 votes vote down vote up
public void writeCsvFromBoundingBox( Double[] boundingBox, Integer gridDistance, Integer grid, String prefix  ) {


		ResultSet results = getSigridForBoundingBox(boundingBox, gridDistance, grid);
		csv.initializeStore(gridDistance, prefix);
		try {
			while(results.next()) {
				csv.savePlot(
						results.getInt("ycoordinate") * 1d /JDBCStore.SCALING_FACTOR *1d, 
						results.getInt("xcoordinate") * 1d  /JDBCStore.SCALING_FACTOR * 1d , 
						results.getInt("row"), 
						results.getInt("col")
						);
			}
		} catch (SQLException e) {
			//logger.error("Error readig results from DB", e);
		} finally {
			csv.closeStore();
			database.closeStore();
		}
	}
 
Example 40
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 41
Source File: WriteCSVStepTest.java    From pipeline-utility-steps-plugin with MIT License 6 votes vote down vote up
@Test
public void writeFileWithHeader() throws Exception {
    File output = temp.newFile();

    WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition(
            "node {\n" +
            "  def recordsString = \"a,b,c\\n1,2,3\\n\"\n" +
            "  List<CSVRecord> records = readCSV text: recordsString\n" +
            "  def format = CSVFormat.EXCEL.withHeader('key', 'value', 'attr')\n" +
            "  writeCSV file: '" + FilenameTestsUtils.toPath(output) + "', records: records, format: format\n" +
            "}", true));
    j.assertBuildStatusSuccess(p.scheduleBuild2(0));

    // file exists by default so we check that should not be empty
    assertThat(output.length(), greaterThan(0l));

    String lines = new String(Files.readAllBytes(Paths.get(output.toURI())));
    assertThat(lines.split("\r\n|\r|\n").length, equalTo(3));
}
 
Example 42
Source File: CsvTrackWriter.java    From mytracks with Apache License 2.0 6 votes vote down vote up
/**
 * Writes a single line of a CSV file.
 * 
 * @param values the values to be written as CSV
 */
private void writeCommaSeparatedLine(String... values) {
  StringBuilder builder = new StringBuilder();
  boolean isFirst = true;
  for (String value : values) {
    if (!isFirst) {
      builder.append(',');
    }
    isFirst = false;

    builder.append('"');
    if (value != null) {
      builder.append(value.replaceAll("\"", "\"\""));
    }
    builder.append('"');
  }
  printWriter.println(builder.toString());
}
 
Example 43
Source File: TracerTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** */
@Test
public void testWriteVectorToCSVFile() throws IOException {
    DenseVector vector = new DenseVector(MathTestConstants.STORAGE_SIZE);

    for (int i = 0; i < vector.size(); i++)
        vector.set(i, Math.random());

    Path file = createTempFile("vector", ".csv");

    Tracer.saveAsCsv(vector, DEFAULT_FORMAT, file.toString());

    System.out.println("Vector exported: " + file.getFileName());

    List<String> strings = Files.readAllLines(file);
    Optional<String> reduce = strings.stream().reduce((s1, s2) -> s1 + s2);
    String[] csvVals = reduce.orElse("").split(",");

    for (int i = 0; i < vector.size(); i++) {
        Double csvVal = Double.valueOf(csvVals[i]);

        assertEquals("Unexpected value.", csvVal, vector.get(i), DEFAULT_DELTA);
    }

    Files.deleteIfExists(file);
}
 
Example 44
Source File: IndexerUtilities.java    From samantha with MIT License 6 votes vote down vote up
public static void writeCSVFields(JsonNode entity, List<String> curFields, BufferedWriter writer,
                                  String separator) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    List<String> fields = new ArrayList<>(curFields.size());
    for (String field : curFields) {
        String value;
        if (!entity.has(field)) {
            logger.warn("The field {} is not present in {}. Filled in with empty string.",
                    field, entity);
            value = mapper.writeValueAsString("");
        } else {
            value = mapper.writeValueAsString(entity.get(field));
        }
        if (value.contains(separator)) {
            logger.warn("The field {} from {} already has the separator {}. Removed.",
                    field, entity, separator);
            value = value.replace(separator, "");
        }
        fields.add(StringEscapeUtils.unescapeCsv(value));
    }
    String line = StringUtils.join(fields, separator);
    writer.write(line);
    writer.newLine();
    writer.flush();
}
 
Example 45
Source File: SpreadsheetDataFileWriterCsv.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public void writeDataToResponse(List<List<Object>> spreadsheetData, String fileName, HttpServletResponse response) {
	response.setContentType("text/comma-separated-values");
	SpreadsheetUtil.setEscapedAttachmentHeader(response, fileName + ".csv");

	String csvString = getAsCsv(spreadsheetData);
	response.setContentLength(csvString.length());
	OutputStream out = null;
	try {
		out = response.getOutputStream();
		out.write(csvString.getBytes("UTF-8"));
		out.flush();
	} catch (IOException e) {
		log.error(e.getMessage());
	} finally {
		try {
			if (out != null) out.close();
		} catch (IOException e) {
			log.error(e.getMessage());
		}
	}
}
 
Example 46
Source File: CSVFormatter.java    From winter with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the data set to a CSV file
 * 
 * @param file
 * @param dataset
 * @throws IOException
 */
public void writeCSV(File file, Processable<RecordType> dataset) throws IOException {
	CSVWriter writer = new CSVWriter(new FileWriter(file));

	String[] headers = getHeader();
	if (headers != null) {
		writer.writeNext(headers);
	}

	for (RecordType record : dataset.get()) {
		String[] values = format(record);

		writer.writeNext(values);
	}

	writer.close();
}
 
Example 47
Source File: CSVWriter.java    From diirt with MIT License 6 votes vote down vote up
/**
 * Writes each row as a set of entries in the .CSV file and
 * outputs a new line between each row.
 * Essentially, each row contains items that represent the column
 * entries within that row.
 * @param csvFile file to write to
 * @param rows cells to write
 */
public static void writeData(File csvFile, List<List> rows){
    //Invalid File
    if (csvFile == null){
        throw new IllegalArgumentException("Cannot write to a null file.");
    }

    //Invalid data
    if (rows == null){
        throw new IllegalArgumentException("Must have non-null data to write.");
    }

    //Writes all rows
    for (List<Object> row: rows){
        writeRow(csvFile, row);
    }
}
 
Example 48
Source File: CSVMatrixWriterTest.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteCells() {
    try (CSVMatrixWriter e = new CSVMatrixWriter(new FileOutputStream(tmpFile))) {
        SimpleCell[] cells = new SimpleCell[10];
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                byte[] forString = new byte[32];
                new Random().nextBytes(forString);
                cells[j] = new SimpleCell(new String(forString));
            }
            e.writeCells(cells);
        }
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
    if (tmpFile.exists()) {
        if (tmpFile.length() == 0L) {
            fail("File is empty");
        }
    } else {
        fail("File not exists!");
    }
}
 
Example 49
Source File: UnlabeledExperimentEval.java    From upparse with Apache License 2.0 6 votes vote down vote up
public void writeSummaryWithLenCSV(PrintStream out) {
  int 
  tp = sum(getTPcounts()),
  fp = sum(getFPcounts()),
  fn = sum(getFNcounts()),
  predCount = tp + fp,
  predLen = getFPlen() + getFPlen();

double
  predCountF = (double) predCount,
  predLenF = (double) predLen,
  predLenAvg = predLenF / predCountF,
  tpF = (double) tp, 
  fpF = (double) fp, 
  fnF = (double) fn,
  prec = 100 * tpF / (tpF + fpF),
  rec = 100 * tpF / (tpF + fnF),
  f = 2 * prec * rec / (prec + rec);
  
out.println(String.format(
  "%.1f,%.1f,%.1f,%.2f", prec, rec, f, predLenAvg));
}
 
Example 50
Source File: TestFileSystemOutput.java    From envelope with Apache License 2.0 6 votes vote down vote up
@Test
public void writeCsvWithOptions() throws Exception {
  Map<String, Object> paramMap = new HashMap<>();
  paramMap.put(FileSystemOutput.FORMAT_CONFIG, "csv");
  paramMap.put(FileSystemOutput.PATH_CONFIG, results.getPath());
  paramMap.put(FileSystemOutput.CSV_HEADER_CONFIG, true);
  config = ConfigFactory.parseMap(paramMap);

  FileSystemOutput fileSystemOutput = new FileSystemOutput();
  assertNoValidationFailures(fileSystemOutput, config);
  fileSystemOutput.configure(config);
  fileSystemOutput.applyBulkMutations(plannedRows);

  File[] files = results.listFiles(new FilenameFilter() {
    @Override
    public boolean accept(File dir, String name) {
      return name.endsWith(".csv");
    }
  });
  assertEquals("Incorrect number of CSV files", 1, files.length);

  BufferedReader br = new BufferedReader(new FileReader(files[0]));
  String line = br.readLine();
  assertEquals("Invalid header", "field1,field2,field3,field4", line);
}
 
Example 51
Source File: TestFileSystemOutput.java    From envelope with Apache License 2.0 6 votes vote down vote up
@Test
public void writeCsvNoOptions() throws Exception {
  Map<String, Object> paramMap = new HashMap<>();
  paramMap.put(FileSystemOutput.FORMAT_CONFIG, "csv");
  paramMap.put(FileSystemOutput.PATH_CONFIG, results.getPath());
  config = ConfigFactory.parseMap(paramMap);

  FileSystemOutput fileSystemOutput = new FileSystemOutput();
  assertNoValidationFailures(fileSystemOutput, config);
  fileSystemOutput.configure(config);
  fileSystemOutput.applyBulkMutations(plannedRows);

  File[] files = results.listFiles(new FilenameFilter() {
    @Override
    public boolean accept(File dir, String name) {
      return name.endsWith(".csv");
    }
  });
  assertEquals("Incorrect number of CSV files", 1, files.length);

  BufferedReader br = new BufferedReader(new FileReader(files[0]));
  String line = br.readLine();
  assertEquals("Invalid header", "0,zero,true,dog", line);
}
 
Example 52
Source File: CSVWriter.java    From elexis-3-core with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Writes the entire ResultSet to a CSV file.
 * 
 * The caller is responsible for closing the ResultSet.
 * 
 * @param rs
 *            the recordset to write
 * @param includeColumnNames
 *            true if you want column names in the output, false otherwise
 * 
 */
public void writeAll(java.sql.ResultSet rs, boolean includeColumnNames) throws SQLException,
	IOException{
	
	ResultSetMetaData metadata = rs.getMetaData();
	
	if (includeColumnNames) {
		writeColumnNames(metadata);
	}
	
	int columnCount = metadata.getColumnCount();
	
	while (rs.next()) {
		String[] nextLine = new String[columnCount];
		
		for (int i = 0; i < columnCount; i++) {
			nextLine[i] = getColumnValue(rs, metadata.getColumnType(i + 1), i + 1);
		}
		
		writeNext(nextLine);
	}
}
 
Example 53
Source File: PointSubsetWriterCSV.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void writePoint(PointFeature pointFeat) throws IOException {
  EarthLocation loc = pointFeat.getLocation();

  writer.print(CalendarDateFormatter.toDateTimeStringISO(pointFeat.getObservationTimeAsCalendarDate()));
  writer.print(',');
  writer.print(Format.dfrac(loc.getLatitude(), 3));
  writer.print(',');
  writer.print(Format.dfrac(loc.getLongitude(), 3));

  StructureData structureData = pointFeat.getDataAll();
  for (VariableSimpleIF wantedVar : wantedVariables) {
    writer.print(',');
    Array dataArray = structureData.getArray(wantedVar.getShortName());
    writer.print(dataArray.toString().trim());
  }
  writer.println();
}
 
Example 54
Source File: StationSubsetWriterCSV.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected void writeStationPointFeature(StationPointFeature stationPointFeat) throws IOException {
  Station station = stationPointFeat.getStation();

  writer.print(CalendarDateFormatter.toDateTimeStringISO(stationPointFeat.getObservationTimeAsCalendarDate()));
  writer.print(',');
  writer.print(station.getName());
  writer.print(',');
  writer.print(Format.dfrac(station.getLatitude(), 3));
  writer.print(',');
  writer.print(Format.dfrac(station.getLongitude(), 3));

  for (VariableSimpleIF wantedVar : wantedVariables) {
    writer.print(',');
    Array dataArray = stationPointFeat.getDataAll().getArray(wantedVar.getShortName());
    writer.print(dataArray.toString().trim());
  }
  writer.println();
}
 
Example 55
Source File: GTableToCSV.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private static void writeColumnNames(PrintWriter writer, List<TableColumn> columns,
		TableModel model, TaskMonitor monitor) {

	monitor.setMessage("Writing columns...");
	monitor.initialize(columns.size());

	for (int i = 0; i < columns.size(); ++i) {
		if (monitor.isCancelled()) {
			break;
		}
		monitor.setProgress(i);

		TableColumn column = columns.get(i);
		int modelIndex = column.getModelIndex();
		writeField(writer, model.getColumnName(modelIndex), monitor);
		if (i < columns.size() - 1) {
			writeFieldSeparator(writer);
		}
	}
}
 
Example 56
Source File: CsvOutput.java    From Strata with Apache License 2.0 6 votes vote down vote up
/**
 * Writes a single cell to the current line.
 * <p>
 * The boolean flag controls whether each entry is always quoted or only quoted when necessary.
 * <p>
 * When using this method, either {@link #writeNewLine()} or one of the {@code writeLine}
 * methods must be called at the end of the line.
 *
 * @param cell  the cell to write
 * @param alwaysQuote  when true, the cell will be quoted, when false, quoting is selective
 * @return this, for method chaining
 * @throws UncheckedIOException if an IO exception occurs
 */
public CsvOutput writeCell(String cell, boolean alwaysQuote) {
  try {
    if (lineStarted) {
      underlying.append(separator);
    }
    if (alwaysQuote || isQuotingRequired(cell)) {
      outputQuotedCell(cell);
    } else {
      underlying.append(cell);
    }
    lineStarted = true;
  } catch (IOException ex) {
    throw new UncheckedIOException(ex);
  }
  return this;
}
 
Example 57
Source File: SensitivityCsvWriter.java    From Strata with Apache License 2.0 6 votes vote down vote up
private void write(
    String reference,
    CurveSensitivitiesType type,
    CurveSensitivities curveSens,
    CurrencyParameterSensitivity paramSens,
    List<String> additionalHeaders,
    boolean containsDates,
    CsvOutput csv) {

  List<String> additionalCells = supplier.values(additionalHeaders, curveSens, paramSens);
  for (int i = 0; i < paramSens.getParameterCount(); i++) {
    ParameterMetadata pmd = paramSens.getParameterMetadata(i);
    Tenor tenor = ((TenoredParameterMetadata) pmd).getTenor();
    double value = paramSens.getSensitivity().get(i);
    csv.writeCell(reference);
    csv.writeCell(type.getName());
    csv.writeCell(tenor.toString());
    if (containsDates) {
      csv.writeCell(pmd instanceof DatedParameterMetadata ? ((DatedParameterMetadata) pmd).getDate().toString() : "");
    }
    csv.writeCell(paramSens.getCurrency().getCode());
    csv.writeCell(BigDecimal.valueOf(value).toPlainString());
    csv.writeLine(additionalCells);
  }
}
 
Example 58
Source File: FxSingleTradeCsvPlugin.java    From Strata with Apache License 2.0 6 votes vote down vote up
void writeProduct(CsvRowOutputWithHeaders csv, String prefix, FxSingle product) {
  Payment basePayment = product.getBaseCurrencyPayment();
  csv.writeCell(prefix + LEG_1_DIRECTION_FIELD, PayReceive.ofSignedAmount(basePayment.getAmount()));
  csv.writeCell(prefix + LEG_1_CURRENCY_FIELD, basePayment.getCurrency());
  csv.writeCell(prefix + LEG_1_NOTIONAL_FIELD, basePayment.getAmount());
  csv.writeCell(prefix + LEG_1_PAYMENT_DATE_FIELD, basePayment.getDate());
  Payment counterPayment = product.getCounterCurrencyPayment();
  csv.writeCell(prefix + LEG_2_DIRECTION_FIELD, PayReceive.ofSignedAmount(counterPayment.getAmount()));
  csv.writeCell(prefix + LEG_2_CURRENCY_FIELD, counterPayment.getCurrency());
  csv.writeCell(prefix + LEG_2_NOTIONAL_FIELD, counterPayment.getAmount());
  csv.writeCell(prefix + LEG_2_PAYMENT_DATE_FIELD, counterPayment.getDate());
  product.getPaymentDateAdjustment().ifPresent(bda -> {
    csv.writeCell(PAYMENT_DATE_CAL_FIELD, bda.getCalendar());
    csv.writeCell(PAYMENT_DATE_CNV_FIELD, bda.getConvention());
  });
}
 
Example 59
Source File: RatesCurvesCsvLoader.java    From Strata with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the curve settings in a CSV format to an appendable.
 * 
 * @param underlying  the underlying appendable destination
 * @param group  the curve group
 */
public static void writeCurveSettings(Appendable underlying, RatesCurveGroup group) {
  CsvOutput csv = CsvOutput.standard(underlying);
  // header
  csv.writeLine(HEADERS_SETTINGS);
  // rows
  Map<Currency, Curve> discountingCurves = group.getDiscountCurves();
  Set<CurveName> names = new HashSet<>();
  for (Entry<Currency, Curve> entry : discountingCurves.entrySet()) {
    Curve curve = entry.getValue();
    csv.writeLine(curveSettings(curve));
    names.add(curve.getName());
  }
  Map<Index, Curve> forwardCurves = group.getForwardCurves();
  for (Entry<Index, Curve> entry : forwardCurves.entrySet()) {
    Curve curve = entry.getValue();
    if (!names.contains(curve.getName())) {
      csv.writeLine(curveSettings(curve));
      names.add(curve.getName());
    }
  }
}
 
Example 60
Source File: RecordSetUtil.java    From components with Apache License 2.0 6 votes vote down vote up
public static String writeRandomCsvFile(FileSystem fs, String path, long seed, int startId, int lines, int columns,
        int tagLength, String fieldDelimiter, String recordDelimiter) throws IOException {
    Random r = new Random(seed);
    try (PrintWriter w = new PrintWriter(fs.create(new Path(path)))) {
        for (int id = 0; id < lines; id++) {
            w.print(startId + id);
            for (int column = 1; column < columns; column++) {
                w.print(fieldDelimiter);
                for (int i = 0; i < tagLength; i++)
                    w.print(CHARS[r.nextInt(CHARS.length)]);
            }
            w.print(recordDelimiter);
        }
    }
    return DFSTestUtil.readFile(fs, new Path(path));
}