Class CsvWriter
- java.lang.Object
-
- cdc.util.csv.CsvWriter
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public class CsvWriter extends Object implements Flushable, Closeable
Utility used to write CSV files.Cell separator can be controlled.
End of line can be controlled.
Escaping end multi line cells are supported.- Author:
- Damien Carbonne
-
-
Field Summary
Fields Modifier and Type Field Description static charDEFAULT_SEPARATORThe default separator that should be used.
-
Constructor Summary
Constructors Constructor Description CsvWriter(File file)CsvWriter(File file, boolean append)CsvWriter(File file, String charset)CsvWriter(File file, String charset, boolean append)Creates a CSvWriter from a file.CsvWriter(PrintStream s)Creates a CSvWriter from a PrintStream.CsvWriter(PrintStream s, String charset)Creates a CSvWriter from a PrintStream.CsvWriter(Writer writer)Create a CsvWriter from a writer.CsvWriter(String filename)CsvWriter(String filename, boolean append)CsvWriter(String filename, String charset)CsvWriter(String filename, String charset, boolean append)Creates a CSvWriter from a file name.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidflush()StringgetLineSeparator()chargetSeparator()CsvWritersetLineSeparator(String sep)Sets the line separator to use.CsvWritersetSeparator(char sep)Sets the cell separator.CsvWriterwrite(boolean value)Writes a boolean cell.CsvWriterwrite(boolean forceEscape, String... values)Writes an array of String cells and controls their escaping.CsvWriterwrite(byte value)Writes a byte cell.CsvWriterwrite(double value)Writes a double cell.CsvWriterwrite(float value)Writes a float cell.CsvWriterwrite(int value)Writes an int cell.CsvWriterwrite(long value)Writes a long cell.CsvWriterwrite(short value)Writes a short cell.<E extends Enum<E>>
CsvWriterwrite(E value)Writes an enum cell.CsvWriterwrite(CharSequence value)Writes a CharSequence cell, escaping it if necessary.CsvWriterwrite(CharSequence value, boolean forceEscape)Writes a CharSequence cell and controls its escaping.CsvWriterwrite(String value)Writes a String cell, escaping it if necessary.CsvWriterwrite(String... values)Writes an array of String cells, escaping them if necessary.CsvWriterwrite(String value, boolean forceEscape)Writes a String cell and controls its escaping.CsvWriterwrite(Collection<String> values)Writes a collection of String cells, escaping them if necessary.CsvWriterwrite(Collection<String> values, boolean forceEscape)Writes a collection of String cells and controls their escaping.CsvWriterwriteln()Writes the line separator (moves to next line).CsvWriterwriteln(boolean forceEscape, String... values)Writes an array of String cells, controlling their escaping, then moves to next line.CsvWriterwriteln(Row row)CsvWriterwriteln(String... values)Writes an array of String cells, escaping if necessary, then moves to next line.CsvWriterwriteln(Collection<String> values)Writes a collection of cells, escaping them if necessary, then moves to next line.CsvWriterwriteln(Collection<String> values, boolean forceEscape)Writes a collection of String cells, controlling their escaping, then moves to next line.
-
-
-
Field Detail
-
DEFAULT_SEPARATOR
public static final char DEFAULT_SEPARATOR
The default separator that should be used.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CsvWriter
public CsvWriter(Writer writer)
Create a CsvWriter from a writer.- Parameters:
writer- The writer.
-
CsvWriter
public CsvWriter(PrintStream s, String charset)
Creates a CSvWriter from a PrintStream.Buffering is used.
- Parameters:
s- The PrintStream.charset- The charset name.
-
CsvWriter
public CsvWriter(PrintStream s)
Creates a CSvWriter from a PrintStream.Buffering is used.
- Parameters:
s- The PrintStream.
-
CsvWriter
public CsvWriter(String filename, String charset, boolean append) throws IOException
Creates a CSvWriter from a file name.Buffering is used.
- Parameters:
filename- The file name.charset- The charset name.append- If true, then bytes will be written to the end of the file rather than the beginning.- Throws:
IOException- When an IO error occurs.
-
CsvWriter
public CsvWriter(String filename, String charset) throws IOException
- Throws:
IOException
-
CsvWriter
public CsvWriter(String filename, boolean append) throws IOException
- Throws:
IOException
-
CsvWriter
public CsvWriter(String filename) throws IOException
- Throws:
IOException
-
CsvWriter
public CsvWriter(File file, String charset, boolean append) throws IOException
Creates a CSvWriter from a file.Buffering is used.
- Parameters:
file- The file.charset- The charset name.append- If true, then bytes will be written to the end of the file rather than the beginning.- Throws:
IOException- When an IO error occurs.
-
CsvWriter
public CsvWriter(File file, String charset) throws IOException
- Throws:
IOException
-
CsvWriter
public CsvWriter(File file, boolean append) throws IOException
- Throws:
IOException
-
CsvWriter
public CsvWriter(File file) throws IOException
- Throws:
IOException
-
-
Method Detail
-
getSeparator
public char getSeparator()
- Returns:
- The used cell separator.
-
setSeparator
public CsvWriter setSeparator(char sep)
Sets the cell separator.- Parameters:
sep- The separator.- Returns:
- This CsvWriter.
-
getLineSeparator
public String getLineSeparator()
- Returns:
- The used line separator.
-
setLineSeparator
public CsvWriter setLineSeparator(String sep)
Sets the line separator to use.- Parameters:
sep- The line separator.- Returns:
- This CsvWriter.
-
writeln
public CsvWriter writeln() throws IOException
Writes the line separator (moves to next line).- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
write
public CsvWriter write(CharSequence value, boolean forceEscape) throws IOException
Writes a CharSequence cell and controls its escaping.If necessary, a cell separator is first appended.
- Parameters:
value- The cell.forceEscape- If true, cell is escaped. If false, cell is escaped if necessary.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
write
public CsvWriter write(CharSequence value) throws IOException
Writes a CharSequence cell, escaping it if necessary.If necessary, a cell separator is first appended.
- Parameters:
value- The cell.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
write
public CsvWriter write(String value, boolean forceEscape) throws IOException
Writes a String cell and controls its escaping.If necessary, a cell separator is first appended.
- Parameters:
value- The cell.forceEscape- If true, cell is escaped. If false, cell is escaped if necessary.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
write
public CsvWriter write(String value) throws IOException
Writes a String cell, escaping it if necessary.If necessary, a cell separator is first appended.
- Parameters:
value- The cell.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
write
public CsvWriter write(Collection<String> values, boolean forceEscape) throws IOException
Writes a collection of String cells and controls their escaping.If necessary, a cell separator is first appended. A cell separator is inserted between consecutive cells.
- Parameters:
values- The cells.forceEscape- If true, all cells are escaped. If false, each cell is escaped if necessary.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
write
public CsvWriter write(Collection<String> values) throws IOException
Writes a collection of String cells, escaping them if necessary.If necessary, a cell separator is first appended.
A cell separator is inserted between consecutive cells.
Cells are escaped if necessary.- Parameters:
values- The cells.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
writeln
public CsvWriter writeln(Collection<String> values, boolean forceEscape) throws IOException
Writes a collection of String cells, controlling their escaping, then moves to next line.If necessary, a cell separator is first appended. A cell separator is inserted between consecutive cells.
- Parameters:
values- The cells.forceEscape- If true, all cells are escaped. If false, each cell is escaped if necessary.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
writeln
public CsvWriter writeln(Collection<String> values) throws IOException
Writes a collection of cells, escaping them if necessary, then moves to next line.If necessary, a cell separator is first appended.
A cell separator is inserted between consecutive cells.
Cells are escaped if necessary.- Parameters:
values- The cells.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
writeln
public CsvWriter writeln(Row row) throws IOException
- Throws:
IOException
-
write
public CsvWriter write(boolean forceEscape, String... values) throws IOException
Writes an array of String cells and controls their escaping.If necessary, a cell separator is first appended. A cell separator is inserted between consecutive cells.
- Parameters:
forceEscape- If true, all cells are escaped. If false, each cell is escaped if necessary.values- The cells.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
write
public CsvWriter write(String... values) throws IOException
Writes an array of String cells, escaping them if necessary.If necessary, a cell separator is first appended. A cell separator is inserted between consecutive cells.
- Parameters:
values- The cells.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
writeln
public CsvWriter writeln(boolean forceEscape, String... values) throws IOException
Writes an array of String cells, controlling their escaping, then moves to next line.If necessary, a cell separator is first appended. A cell separator is inserted between consecutive cells.
- Parameters:
forceEscape- If true, all cells are escaped. If false, each cell is escaped if necessary.values- The cells.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
writeln
public CsvWriter writeln(String... values) throws IOException
Writes an array of String cells, escaping if necessary, then moves to next line.If necessary, a cell separator is first appended. A cell separator is inserted between consecutive cells.
- Parameters:
values- The cells.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
write
public CsvWriter write(boolean value) throws IOException
Writes a boolean cell.If necessary, a cell separator is first appended.
- Parameters:
value- The cell.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
write
public CsvWriter write(long value) throws IOException
Writes a long cell.If necessary, a cell separator is first appended.
- Parameters:
value- The cell.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
write
public CsvWriter write(int value) throws IOException
Writes an int cell.If necessary, a cell separator is first appended.
- Parameters:
value- The cell.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
write
public CsvWriter write(short value) throws IOException
Writes a short cell.If necessary, a cell separator is first appended.
- Parameters:
value- The cell.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
write
public CsvWriter write(byte value) throws IOException
Writes a byte cell.If necessary, a cell separator is first appended.
- Parameters:
value- The cell.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
write
public CsvWriter write(double value) throws IOException
Writes a double cell.If necessary, a cell separator is first appended.
- Parameters:
value- The cell.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
write
public CsvWriter write(float value) throws IOException
Writes a float cell.If necessary, a cell separator is first appended.
- Parameters:
value- The cell.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
write
public <E extends Enum<E>> CsvWriter write(E value) throws IOException
Writes an enum cell.If necessary, a cell separator is first appended.
- Type Parameters:
E- The enum type.- Parameters:
value- The cell.- Returns:
- This CsvWriter.
- Throws:
IOException- When an IO error occurs.
-
flush
public void flush() throws IOException- Specified by:
flushin interfaceFlushable- Throws:
IOException
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
-