This CsvWriter has been created to work particularly well with the screen-scraper objects. It is simple to use and provided to ease the task of keeping track of everything when creating a csv file.
The most used methods are documented here but if you would like more information you can read the JavaDoc for the CsvWriter.
CsvWriter
CsvWriter CsvWriter ( String filePath ) (professional and enterprise editions only)
CsvWriter CsvWriter ( String filePath, boolean addTimeStamp ) (professional and enterprise editions only)
CsvWriter CsvWriter ( String filePath, char separator ) (professional and enterprise editions only)
CsvWriter CsvWriter ( String filePath, char separator, boolean addTimeStamp ) (professional and enterprise editions only)
CsvWriter CsvWriter ( String filePath, char separator, char quotechar ) (professional and enterprise editions only)
CsvWriter CsvWriter ( String filePath, char separator, char quotechar, char escapechar ) (professional and enterprise editions only)
CsvWriter CsvWriter ( String filePath, char separator, char quotechar, String lineEnd ) (professional and enterprise editions only)
CsvWriter CsvWriter ( String filePath, char separator, char quotechar, char escapechar, String lineEnd ) (professional and enterprise editions only)
Description
Create a csv file writer.
Parameters
- filePath File path to where the csv file should be created/saved, as a string.
- addTimeStamp (optional) If true a time stamp will be added to the filename; otherwise, the filePath will remain unchanged.
- seperator (optional) The character that should be used to separate the fields in the csv file, the default is char 44 (comma).
- quotechar (optional) The character that should be used to quote fields, the default is char 34 (straight double-quotes).
- escapechar (optional) The escape character for quotes, the default is char 34 (straight double-quotes).
- lineEnd (optional) The end of line character, as a string. The default is the new line character ("\n").
Return Values
Returns a CsvWriter object. If it encounters an error it will be thrown.
Change Log
Version |
Description |
5.0 |
Available for Professional and Enterprise editions. |
4.5.18a |
Introduced in alpha version. |
Class Location
com.screenscraper.csv.CsvWriter
Examples
Create CsvWriter
// Import class
import com.screenscraper.csv.*;
// Create CsvWriter with timestamp
CsvWriter writer = new CsvWriter("output.csv", true);
// Save in session variable for general access
session.setVariable( "WRITER", writer);
setHeader
void csvWriter.setHeader ( String[ ] header )
Description
Set the header row of the csv document. If the document already exists the headers will not be written. Also creates a data record mapping to ease writing to file.
Parameters
- header Headers of csv file, as a one-dimensional array of strings.
Return Values
Returns void.
Change Log
Version |
Description |
5.0 |
Available for all editions. |
4.5.18a |
Introduced in alpha version. |
If you want to use the data record mapping then the extractor tokens names should be all caps and all spaces should be replaced with underscores.
Examples
Add Headers to CSV File
// Create CsvWriter with timestamp
CsvWriter writer = new CsvWriter("output.csv", true);
// Create Headers Array
String[] header = {"Brand Name", "Product Title"};
// Set Headers
writer.setHeader(header);
// Write out to file
writer.flush();
// Save in session variable for general access
session.setVariable( "WRITER", writer);
write
void csvWriter.write ( DataRecord dataRecord )
Description
Write to the CsvWriter object.
Parameters
- dataRecord The data record containing the mapped token matches (see setHeader). Note that the token names in the data record should be in all caps, and spaces should be replaced with underscores. For example, if one of your headers is "Product ID", the corresponding data record token should be "PRODUCT_ID". This is in keeping with the recommended naming convention for extractor pattern tokens.
Return Values
Returns void.
Change Log
Version |
Description |
5.0 |
Available for all editions. |
4.5.18a |
Introduced in alpha version. |
Examples
Write Data Record to CSV
// Retrieve CsvWriter from session variable
writer = session.getv( "WRITER" );
// Write dataRecord to the file (headers already set)
writer.write(dataRecord);
// Flush record to file (write it now)
writer.flush();