olg.csv.base.csv
Class CSVWriter

Package class diagram package CSVWriter
java.lang.Object
  extended by olg.csv.base.csv.CSVWriter
All Implemented Interfaces:
Closeable, IWriter

public class CSVWriter
extends Object
implements IWriter

CSV writer. CSV Implementation of IWriter.


Field Summary
private  String charsetName
          Charset.
private  int currentLine
          the index of the current line.
private  String ddelimiter
          Double Delimiter.
private  char delimiter
          Cell delimiter.
private  boolean forceDelimiter
          Default value is CSVSettings.DEFAULT_FORCEDELIMITER.
private  String lineSeparator
          Line break.
private static org.slf4j.Logger LOGGER
          The logger.
private  FileOutputStream outFile
          the out file.
private  OutputStreamWriter outw
          the Output writer.
private  char separator
          Cell separator.
private  int size
          the size of each CSV row.
private  boolean withHeaders
          indicate if writing an header line is required?
 
Constructor Summary
CSVWriter(File outFile, CSVSettings settings)
          Constructor.
CSVWriter(OutputStream out, CSVSettings settings)
          Constructor.
 
Method Summary
 void addLine(String[] values)
          Adds a row from the given String array.
 void addRow(Row row)
          Adds a row.
private  void checkRow(Row row)
          check if a row has the correct size and its num is >= the current line.
 void close()
          Close this stream and caught and logged IOException. if the parent stream is provided and passed to CSVWriter constructor by user, user must close it (close what you open!)
private  String formatValue(String value)
          add delimiter to a value if necessary or forced.
 boolean isWithHeaders()
          Indicates if the file has a headers line.
private  StringBuilder padding(int begin, int end)
          create a stringBuider with end-begin empty csv fields.
private  void writeBlankLine(int num)
          writes blank rows until reach a specific row (exclude).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOGGER

private static final org.slf4j.Logger LOGGER
The logger.


delimiter

private char delimiter
Cell delimiter. Default value is CSVSettings.DEFAULT_DELIMITER.


ddelimiter

private String ddelimiter
Double Delimiter. Virtue of CSV rfc, delimiter character must be twice in a cell to represent it.


separator

private char separator
Cell separator. Default value is CSVSettings.DEFAULT_SEPARATOR.


lineSeparator

private String lineSeparator
Line break. Default value is CSVSettings.DEFAULT_LINESEPARATOR.


charsetName

private String charsetName
Charset. Default value is CSVSettings.DEFAULT_CHARSETNAME.


withHeaders

private boolean withHeaders
indicate if writing an header line is required? Default value is CSVSettings.DEFAULT_WITHHEADERS.


outw

private final OutputStreamWriter outw
the Output writer.


outFile

private FileOutputStream outFile
the out file.


forceDelimiter

private boolean forceDelimiter
Default value is CSVSettings.DEFAULT_FORCEDELIMITER.


size

private int size
the size of each CSV row.


currentLine

private int currentLine
the index of the current line.

Constructor Detail

CSVWriter

public CSVWriter(OutputStream out,
                 CSVSettings settings)
Constructor.

Parameters:
out - the stream on which to write. Must be not null.
settings - the CSV settings to use to write on the stream. Must be not null and must have a not null lineSeparator setting.
See Also:
for default values

CSVWriter

public CSVWriter(File outFile,
                 CSVSettings settings)
          throws FileNotFoundException
Constructor.

Parameters:
outFile - the file to write. Must be not null.
settings - the CSV settings to use to write on the file. Must be not null and have a not null lineSeparator setting.
Throws:
FileNotFoundException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason
See Also:
for default values
Method Detail

addLine

public void addLine(String[] values)
Adds a row from the given String array. The first time this method define the size ( as the given string array length) used along writing unless IWriter.addRow(Row) had been used first.

Throws a WriterException when line size differs from initial size.

Specified by:
addLine in interface IWriter
Parameters:
values - the row. Must be not null with length >0. .

addRow

public void addRow(Row row)
Adds a row.

The first row allows to define the size used along writing unless IWriter.addLine(String[]) had been used first.

Throws a WriterException when row size differs from initial size or row num is lesser than the expected next number line. Or if I/O error occurs during writing.

Specified by:
addRow in interface IWriter
Parameters:
row - the row. It's possible to use a row with no cells. In this case, an empty line(a line wich all fields are null) is added. It's possible to use a row with a num greater than the expected number. In this case, empty lines are wroten as much as necessary. It's possible to use row which all cells are not specified. In this case the missing cells are added as empty cells. Must be not null with cells correctly ordered and size>0.

close

public void close()
Close this stream and caught and logged IOException. if the parent stream is provided and passed to CSVWriter constructor by user, user must close it (close what you open!)

Specified by:
close in interface Closeable

formatValue

private String formatValue(String value)
add delimiter to a value if necessary or forced.

Parameters:
value - the value
Returns:
the value delimited

writeBlankLine

private void writeBlankLine(int num)
writes blank rows until reach a specific row (exclude).

Parameters:
num - the row num to reach

padding

private StringBuilder padding(int begin,
                              int end)
create a stringBuider with end-begin empty csv fields.

Parameters:
begin - field index from which beginning the padding.
end - field index
Returns:
the stringBuilder

checkRow

private void checkRow(Row row)
check if a row has the correct size and its num is >= the current line.

Parameters:
row - the row.

isWithHeaders

public boolean isWithHeaders()
Indicates if the file has a headers line.

Specified by:
isWithHeaders in interface IWriter
Returns:
true if the file has a headers line.


Copyright © 2014. All rights reserved.