comparison flys-artifacts/src/main/java/de/intevation/flys/exports/StepCSVWriter.java @ 2575:475dd07c2cb1

New utility to handle more dynamically long csv exported rows. flys-artifacts/trunk@4113 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Mon, 05 Mar 2012 09:53:30 +0000
parents
children
comparison
equal deleted inserted replaced
2574:3598690dc9e2 2575:475dd07c2cb1
1 package de.intevation.flys.exports;
2
3 import au.com.bytecode.opencsv.CSVWriter;
4
5 import java.util.ArrayList;
6 import java.util.Arrays;
7
8 /**
9 * Class to overcome shortcoming of CSVWriter to accept String-Arrays only.
10 * The StepCSVWriter buffers incoming values, such that rows in a csv can be
11 * created more dynamically. Do not forget to call flush().
12 */
13 public class StepCSVWriter {
14
15 /** Writer to use when calling flush. */
16 CSVWriter writer = null;
17 /** Buffer of strings (values). */
18 ArrayList<String> buffer;
19
20
21 /** Trivial constructor. */
22 public StepCSVWriter() {
23 buffer = new ArrayList<String>();
24 }
25
26
27 /** Set writer. */
28 public void setCSVWriter(CSVWriter writer) {
29 this.writer = writer;
30 }
31
32
33 /** Add a value to next flush. */
34 public void addNext(String value) {
35 buffer.add(value);
36 }
37
38
39 /** Add many values to next flush. */
40 public void addNexts(String ... values) {
41 buffer.addAll(Arrays.asList(values));
42 }
43
44
45 /** Write the row with csvwriter. */
46 public void flush() {
47 writer.writeNext(buffer.toArray(new String[buffer.size()]));
48 buffer.clear();
49 }
50 }
51 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org