如何使用Jackson CSV(https://github.com/FasterXML/jackson-dataformats-text/tree/master/csv)将数据附加到现有CSV文件 这是我的代码
public synchronized Boolean AppendToFile(Path fullFilePath, InputDataFormat obj) throws IOException {
if (mapper == null || schema == null)
init();
File outputFile = new File(fullFilePath.toUri());
if (!outputFile.exists()) {
outputFile.createNewFile();
}
ObjectWriter writer = mapper.writer(schema);
mapper.writer(schema).writeValue(outputFile, obj);
return true;
}
它写入文件但不保留现有数据
答案 0 :(得分:1)
使用OutputStream解决并将append标志传递为true
public Boolean AppendToFile(Path fullFilePath, Account obj) {
logger.info(" Acquired Write lock on file " + fullFilePath.getFileName());
mapper = new CsvMapper();
mapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
schema = mapper.schemaFor(Account.class).withColumnSeparator('|');
File outputFile = new File(fullFilePath.toUri());
if (!outputFile.exists()) {
outputFile.createNewFile();
}
ObjectWriter writer = mapper.writer(schema);
OutputStream outstream = new FileOutputStream(outputFile , true);
writer.writeValue(outstream,obj);
return true;
}