public class Test {
public static void main(String[] args) throws IOException {
String DATA = "\"a\";12";
CSVParser csvParser =
CSVFormat.EXCEL
.withIgnoreEmptyLines()
.withIgnoreHeaderCase()
.withRecordSeparator('\n').withQuote('"')
.withEscape('\\').withDelimeter(';').withTrim()
.parse(new StringReader(DATA));
}
public static void main (String[] args) {
CSVFormat.EXCEL.newFormat(',').withQuote('"')
}
public static void main (String[] args) {
CSVParser parsed = CSVParser.parse(csvData,
CSVFormat.newFormat('\t').withQuote('"').withHeader());
}
CSVFormat.withDelimiter(',').withQuote('"');
CSVFormat
.withDelimiter(',')
.withQuote('"')
.withRecordSeparator("\r\n")
.withIgnoreEmptyLines(false)
.withAllowMissingColumnNames(true);
public void testHeader() throws IOException {
final StringWriter sw = new StringWriter();
try (final CSVPrinter printer = new CSVPrinter(sw,
CSVFormat.DEFAULT.withQuote(null).withHeader("C1", "C2", "C3"))) {
printer.printRecord("a", "b", "c");
printer.printRecord("x", "y", "z");
assertEquals("C1,C2,C3\r\na,b,c\r\nx,y,z\r\n", sw.toString());
}
}
protected static CSVFormat createFormat(final String format, final Character delimiter, final Character escape, final Character quote, final QuoteMode quoteMode, final String nullString, final String recordSeparator) {
CSVFormat csvFormat = CSVFormat.valueOf(format);
if (isNotNul(delimiter)) {
csvFormat = csvFormat.withDelimiter(delimiter);
}
if (isNotNul(escape)) {
csvFormat = csvFormat.withEscape(escape);
}
if (isNotNul(quote)) {
csvFormat = csvFormat.withQuote(quote);
}
if (quoteMode != null) {
csvFormat = csvFormat.withQuoteMode(quoteMode);
}
if (nullString != null) {
csvFormat = csvFormat.withNullString(nullString);
}
if (recordSeparator != null) {
csvFormat = csvFormat.withRecordSeparator(recordSeparator);
}
return csvFormat;
}
public static byte[] exportAsCSV(QueryResult result) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final CSVPrinter csvPrinter;
try {
final CSVFormat format = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.NON_NUMERIC);
csvPrinter = new CSVPrinter(new PrintWriter(out), format);
csvPrinter.printRecord(result.getMetadata().stream().map(SchemaField::getName).collect(Collectors.toList()));
csvPrinter.printRecords(Iterables.transform(result.getResult(), input -> Iterables.transform(input, input1 -> {
if (input1 instanceof List || input1 instanceof Map) {
return JsonHelper.encode(input1);
}
if (input1 instanceof byte[]) {
return DatatypeConverter.printBase64Binary((byte[]) input1);
}
return input1;
})));
csvPrinter.flush();
} catch (IOException e) {
throw Throwables.propagate(e);
}
return out.toByteArray();
}
public CSVFormat build() {
CSVFormat format = newFormat(delimiter = initDelimiter());
format = format.withEscape(escape = initEscape());
format = format.withCommentMarker(commentMarker = initCommentMarker());
format = format.withRecordSeparator(lineSeparator = initLineSeparator());
quote = initQuote();
quoting = initQuoting();
if (quoting) {
format = format.withQuoteMode(QuoteMode.MINIMAL);
format = format.withQuote(quote);
}
return format;
}
public CSVFormat detectCSVFormat(String sampleText, boolean headerRow) throws IOException {
CSVFormat format = CSVFormat.DEFAULT.withAllowMissingColumnNames();
try (BufferedReader br = new BufferedReader(new StringReader(sampleText))) {
List<LineStats> lineStats = generateStats(br);
Character quote = guessQuote(lineStats);
Character delim = guessDelimiter(lineStats, sampleText, quote, headerRow);
if (delim == null) {
throw new IOException("Unrecognized format");
}
format = format.withDelimiter(delim);
format = format.withQuoteMode(QuoteMode.MINIMAL).withQuote(quote);
}
return format;
}
public CSVFormat getCSVFormat() {
return CSVFormat.DEFAULT
.withDelimiter(delimiter)
.withRecordSeparator(recordSeparator)
.withNullString(nullString)
.withEscape(escapeCharacter)
.withQuote(quoteCharacter)
.withQuoteMode(quoteMode);
}