public static OutputWriter getWriter(String filename, final String[] headers, char separatorChar, char quoteChar,
final InputColumn<?>... columns) {
CsvOutputWriter outputWriter;
synchronized (dataContexts) {
UpdateableDataContext dataContext = dataContexts.get(filename);
if (dataContext == null) {
File file = new File(filename);
File parentFile = file.getParentFile();
if (parentFile != null && !parentFile.exists()) {
parentFile.mkdirs();
}
dataContext = new CsvDataContext(file, getConfiguration(separatorChar, quoteChar));
final Schema schema = dataContext.getDefaultSchema();
dataContext.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback callback) {
TableCreationBuilder tableBuilder = callback.createTable(schema, "table");
for (String header : headers) {
tableBuilder.withColumn(header);
}
tableBuilder.execute();
}
});
Table table = dataContext.getDefaultSchema().getTables()[0];
dataContexts.put(filename, dataContext);
counters.put(filename, new AtomicInteger(1));
outputWriter = new CsvOutputWriter(dataContext, filename, table, columns);
// write the headers
} else {
Table table = dataContext.getDefaultSchema().getTables()[0];
outputWriter = new CsvOutputWriter(dataContext, filename, table, columns);
counters.get(filename).incrementAndGet();
}
}