2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.util
;
18 import java
.io
.IOException
;
19 import java
.io
.OutputStream
;
20 import java
.io
.OutputStreamWriter
;
21 import java
.io
.UnsupportedEncodingException
;
22 import java
.io
.Writer
;
23 import java
.util
.Iterator
;
24 import java
.util
.List
;
26 /** Write in CSV format. */
27 public class CsvWriter
{
28 private final Writer out
;
30 private char separator
= ',';
31 private char quote
= '\"';
34 * Creates a CSV writer.
37 * the stream to write to. Caller is responsible for closing it.
39 public CsvWriter(OutputStream out
) {
40 this.out
= new OutputStreamWriter(out
);
44 * Creates a CSV writer.
47 * the stream to write to. Caller is responsible for closing it.
49 public CsvWriter(OutputStream out
, String encoding
) {
51 this.out
= new OutputStreamWriter(out
, encoding
);
52 } catch (UnsupportedEncodingException e
) {
53 throw new UtilsException("Cannot initialize CSV writer", e
);
58 * Write a CSV line. Also used to write a header if needed (this is
59 * transparent for the CSV writer): simply call it first, before writing the
62 public void writeLine(List
<?
> tokens
) {
64 Iterator
<?
> it
= tokens
.iterator();
65 while (it
.hasNext()) {
66 writeToken(it
.next().toString());
72 } catch (IOException e
) {
73 throw new UtilsException("Could not write " + tokens
, e
);
78 * Write a CSV line. Also used to write a header if needed (this is
79 * transparent for the CSV writer): simply call it first, before writing the
82 public void writeLine(Object
[] tokens
) {
84 for (int i
= 0; i
< tokens
.length
; i
++) {
85 if (tokens
[i
] == null) {
86 // TODO configure how to deal with null
89 writeToken(tokens
[i
].toString());
91 if (i
!= (tokens
.length
- 1))
96 } catch (IOException e
) {
97 throw new UtilsException("Could not write " + tokens
, e
);
101 protected void writeToken(String token
) throws IOException
{
102 // +2 for possible quotes, another +2 assuming there would be an already
103 // quoted string where quotes needs to be duplicated
104 // another +2 for safety
105 // we don't want to increase buffer size while writing
106 StringBuffer buf
= new StringBuffer(token
.length() + 6);
107 char[] arr
= token
.toCharArray();
108 boolean shouldQuote
= false;
127 if (shouldQuote
== true)
129 out
.write(buf
.toString());
130 if (shouldQuote
== true)
134 public void setSeparator(char separator
) {
135 this.separator
= separator
;
138 public void setQuote(char quote
) {