Java Code Examples for org.supercsv.io.CsvListWriter#close()

The following examples show how to use org.supercsv.io.CsvListWriter#close() . 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: SparseDataCreator.java    From super-csv with Apache License 2.0 6 votes vote down vote up
@Ignore("This is a test only for convenience")
@Test
public void CreateSparseNumericData() throws IOException
{
	FileWriter f = new FileWriter("SparseNumbersOnly.csv"); 
	CsvListWriter ff = new CsvListWriter(f, CsvPreference.STANDARD_PREFERENCE);
	
	final int rowsToProduce = 1000000;
	int j = 33;
	for(int i = 0; i < rowsToProduce; i++)
	{
		if(j == 0) j = 2;
		ff.write(i, i*j, i/(double)j);
		j = i*j % 1843;
	}
	ff.close();
}
 
Example 2
Source File: WritingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
private String writeToCsv(List<String> data, CellProcessor[] processors, CsvPreference preference)
	throws IOException {
	StringWriter writer = new StringWriter();
	CsvListWriter listWriter = new CsvListWriter(writer, preference);
	listWriter.write(data, processors);
	listWriter.close();
	
	return writer.toString();
}
 
Example 3
Source File: ExportCSVWriterBuilderTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void buildCVSWriter() throws IOException {

    // given
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    ExportParams exportParams = ExportParams.withDirectory("/tmp");

    // when
    CsvListWriter csvWriter = csvWriterBuilder.build(byteStream, exportParams);
    csvWriter.write(new String[]{"a1", "b1", "c1", "d1"});
    csvWriter.write(new String[]{"a2", "b 2", "c2", "d2"});      // space in field
    csvWriter.write(new String[]{"a3", "b3", "c3", "d,3"});      // comma in field
    csvWriter.write(new String[]{"a\n4", "b4", "c4", "d4"});     // newline in field
    csvWriter.write(new String[]{"a5", "b\"5", "c5", "d5"});     // quote in field
    csvWriter.write(new String[]{"a5", "b5", "c5\u1272", "d5"}); // multi-byte unicode char in field
    csvWriter.close();

    // then
    assertEquals("" +
                    "a1,b1,c1,d1\n" +
                    "a2,b 2,c2,d2\n" +
                    "a3,b3,c3,\"d,3\"\n" +
                    "\"a\n" +
                    "4\",b4,c4,d4\n" +
                    "a5,\"b\"\"5\",c5,d5\n" +
                    "a5,b5,c5ቲ,d5\n",
            new String(byteStream.toByteArray(), "UTF-8"));

}
 
Example 4
Source File: HistoDbClientImpl.java    From ipst with Mozilla Public License 2.0 4 votes vote down vote up
public String updateRecord(String id, String[] headers, Object[] values) {
    StringWriter sw = new StringWriter();
    CsvListWriter writer = new CsvListWriter(sw, new CsvPreference.Builder('"', ',', "\r\n").build());

    /*
    There's no need to add the _id, as it is passed in the URL

    ArrayList<String> headersList = new ArrayList<>();
    headersList.add("_id");
    headersList.addAll(Arrays.asList(headers));

    ArrayList<Object> valuesList = new ArrayList<>();
    valuesList.add(id);
    valuesList.addAll(Arrays.asList(values));

    writer.writeHeader(headersList.toArray(new String[] {}));
    writer.write(valuesList.toArray());
    */
    String idNonNull = id;
    try {
        writer.writeHeader(headers);
        writer.write(values);

        writer.close();

        // if no id is provided, rely on server-side auto-increment mechanism

        if (idNonNull == null) {
            idNonNull = "autoIncrement";
        }

        try (InputStream is = httpClient.postHttpRequest(new HistoDbUrl(config,
                                                                        "data/" + idNonNull + ".csv", // WARN here one must NOT use the itesla suffix (not supporting POST of new data)
                                                                        Collections.emptyMap()),
                                                         sw.toString().getBytes("UTF-8"))) {
            return new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8);
        }
    } catch (IOException e) {
        throw new RuntimeException("Failed to store network values for id " + idNonNull, e);
    }
}