List of usage examples for com.fasterxml.jackson.dataformat.csv CsvMapper configure
public CsvMapper configure(CsvParser.Feature f, boolean state)
From source file:hrytsenko.gscripts.io.CsvFiles.java
/** * Save records into file./*from w w w .j a v a2 s . c o m*/ * * <p> * If file already exists, then it will be overridden. * * @param records * the list of records to save. * @param args * the named arguments. * * @throws IOException * if file could not be saved. */ public static void saveCsv(List<Map<String, ?>> records, Map<String, ?> args) { if (records.isEmpty()) { LOGGER.info("No records to save."); return; } Path path = NamedArgs.findPath(args); LOGGER.info("Save {}.", path.getFileName()); CsvSchema.Builder csvSchema = schemaFrom(args).setUseHeader(true); Records.columns(records).forEach(csvSchema::addColumn); try (Writer writer = Files.newBufferedWriter(path, charsetFrom(args))) { CsvMapper csvMapper = new CsvMapper(); csvMapper.configure(CsvGenerator.Feature.ALWAYS_QUOTE_STRINGS, true); csvMapper.writer().with(csvSchema.build()).writeValue(writer, Records.normalize(records)); } catch (IOException exception) { throw new AppException(String.format("Could not save file %s.", path.getFileName()), exception); } }
From source file:ro.fortsoft.dada.csv.CsvGenericDao.java
public long readFromCsv() { File file = new File(csvFile); if (!file.exists() || !file.isFile()) { return 0; }/*from w ww. j a v a 2 s . c o m*/ Class<T> persistentClass = getPersistentClass(); // create mapper and schema CsvMapper mapper = new CsvMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); CsvSchema schema = mapper.schemaFor(persistentClass).withHeader(); this.entities = new ArrayList<>(); // read entities long count = 0; try { MappingIterator<T> it = mapper.reader(persistentClass).with(schema).readValues(file); while (it.hasNextValue()) { entities.add(it.nextValue()); count++; } } catch (IOException e) { e.printStackTrace(); } return count; }