io.castled.filemanager.CsvFileWriter Maven / Gradle / Ivy
package io.castled.filemanager;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.QuoteMode;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Supplier;
public class CsvFileWriter {
private final long maxRecords;
private final Path fileDirectory;
private final Supplier fileNameSupplier;
private final List headerFields;
private volatile CSVPrinter csvPrinter;
private long recordCount = 0;
public CsvFileWriter(long maxRecords, Path fileDirectory, Supplier fileNameSupplier,
List headerFields) throws IOException {
this.maxRecords = maxRecords;
this.fileDirectory = fileDirectory;
this.fileNameSupplier = fileNameSupplier;
this.headerFields = headerFields;
if (!Files.exists(fileDirectory)) {
Files.createDirectories(fileDirectory);
}
}
public void writeRecord(List