final URL url = ...;
final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8");
final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());
try {
for (final CSVRecord record : parser) {
final String string = record.get("SomeColumn");
...
}
} finally {
parser.close();
reader.close();
}
final Appendable out = ...;
final CSVPrinter printer = CSVFormat.DEFAULT.withHeader("H1", "H2").print(out)
public void createCSVFile() throws IOException {
FileWriter out = new FileWriter("book_new.csv");
try (CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT
.withHeader(HEADERS))) {
AUTHOR_BOOK_MAP.forEach((author, title) -> {
printer.printRecord(author, title);
});
}
}
Iterable<CSVRecord> records = CSVFormat.DEFAULT
.withHeader("author", "title").parse(in);
for (CSVRecord record : records) {
String author = record.get("author");
String title = record.get("title");
}
enum BookHeaders {
author, title
}
Iterable<CSVRecord> records = CSVFormat.DEFAULT
.withHeader(BookHeaders.class).parse(in);
for (CSVRecord record : records) {
String author = record.get(BookHeaders.author);
String title = record.get(BookHeaders.title);
}
public DataSet(final Reader reader, final CSVFormat input_format) {
this();
try (CSVParser parser = new CSVParser(reader, input_format.withHeader())) {
labels.addAll(getColumnLabels(parser));
for (final CSVRecord record : parser) {
final List<String> items = csvRecordToList(record);
final int size = items.size();
// Don't add row if the line was empty.
if (size > 1 || (size == 1 && items.get(0).length() > 0)) {
records.add(items);
}
}
reader.close();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
public CSVFormat injectHeaderFormat(CSVFormat format)
{
String[] names = new String[headers.length];
int i = 0;
for (Entry header : headers) {
names[i] = header.name;
i += 1;
}
return format.withHeader(names);
}
public static CsvUnmarshaller create(CSVFormat format, CsvDataFormat dataFormat) {
// If we want to use maps, thus the header must be either fixed or automatic
if (dataFormat.isUseMaps() && format.getHeader() == null) {
format = format.withHeader();
}
// If we want to skip the header record it must automatic otherwise it's not working
if (format.getSkipHeaderRecord() && format.getHeader() == null) {
format = format.withHeader();
}
if (dataFormat.isLazyLoad()) {
return new StreamCsvUnmarshaller(format, dataFormat);
}
return new BulkCsvUnmarshaller(format, dataFormat);
}
public static CsvMarshaller create(CSVFormat format, CsvDataFormat dataFormat) {
// If we don't want the header record, clear it
if (format.getSkipHeaderRecord()) {
format = format.withHeader((String[]) null);
}
String[] fixedColumns = dataFormat.getHeader();
if (fixedColumns != null && fixedColumns.length > 0) {
return new FixedColumnsMarshaller(format, fixedColumns);
}
return new DynamicColumnsMarshaller(format);
}
public void setUp()
throws Exception {
FileUtils.forceMkdir(TEMP_DIR);
try (FileWriter fileWriter = new FileWriter(DATA_FILE);
CSVPrinter csvPrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withHeader(COLUMNS))) {
for (Object[] record : RECORDS) {
csvPrinter.printRecord(record[0],
StringUtils.join((int[]) record[1], CSVRecordReaderConfig.DEFAULT_MULTI_VALUE_DELIMITER));
}
}
}