Class CSVWriter
Object
CSVWriter
- All Implemented Interfaces:
AutoCloseable
Writes a CSV file to the file-system.
It can be disabled, whereby no content is written to the file-system but method calls otherwise work as usual.
As it can be called by different threads, public methods are synchronized.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
static CSVWriter
Creates and starts a CSVWriter (it's always allowed, so will never return null)createFromOutputter
(String outputName, OutputterChecked outputter) Creates and starts a CSVWriter if it's allowed, otherwise returnsOptional.empty()
.createFromOutputterWithHeaders
(String outputName, OutputterChecked outputter, Supplier<List<String>> headerNames, ErrorReporter errorReporter) LikecreateFromOutputter(String, OutputterChecked)
but suppresses any exceptions into an error log - and writes headers.boolean
Whether the CSV file writes header-names as the first line.boolean
Whether the output is enabled or not?void
writeHeaders
(List<String> headerNames) Writes a line with the header-names of the columns.void
writeRow
(List<TypedValue> elements) Writes a line with values of a particular row
-
Method Details
-
createFromOutputterWithHeaders
public static Optional<CSVWriter> createFromOutputterWithHeaders(String outputName, OutputterChecked outputter, Supplier<List<String>> headerNames, ErrorReporter errorReporter) LikecreateFromOutputter(String, OutputterChecked)
but suppresses any exceptions into an error log - and writes headers.- Parameters:
outputName
- unique name identifying the output which is used to construct a file-path to write to.outputter
- how and whether outputs are written.headerNames
- header-names for the CSV fileerrorReporter
- used to reporter an error if the output cannot be created.- Returns:
- the csv-writer if it's allowed, or empty if it's not, or if an error occurs.
-
createFromOutputter
public static Optional<CSVWriter> createFromOutputter(String outputName, OutputterChecked outputter) throws OutputWriteFailedException Creates and starts a CSVWriter if it's allowed, otherwise returnsOptional.empty()
.- Parameters:
outputName
- unique name identifying the output which is used to construct a file-path to write to.outputter
- how and whether outputs are written.- Returns:
- the csv-writer if it's allowed, or empty if it's not.
- Throws:
OutputWriteFailedException
- if the CSV file cannot be created successfully.
-
create
Creates and starts a CSVWriter (it's always allowed, so will never return null)- Parameters:
path
- path to write the CSV to- Returns:
- the csv-writer
- Throws:
OutputWriteFailedException
- if the CSV file cannot be created successfully.
-
hasWrittenHeaders
public boolean hasWrittenHeaders()Whether the CSV file writes header-names as the first line.- Returns:
- true iff the CSV file writes headers.
-
isOutputEnabled
public boolean isOutputEnabled()Whether the output is enabled or not?When the output is disabled, no CSV file is written to the file-system, but the public methods work as usual.
- Returns:
- true iff the output is enabled.
-
writeHeaders
Writes a line with the header-names of the columns.This is called zero or one times before calling
writeRow(List)
but the class does not enforce this order.It should not be called more than once.
- Parameters:
headerNames
- a name for each respective column. This should have an identical number of elements aselements
in subsequent calls towriteRow(List)
.
-
writeRow
Writes a line with values of a particular rowThis is always called after optionally calling
writeHeaders(List)
once but the class does not enforce this order.- Parameters:
elements
- a value in the row for each respective column. This should have an identical number of elements each time it is called, and be equal to the number ofelements
in any previous call towriteHeaders(List)
.
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-