Как я могу дать команду CSVPrinter не писать строку заголовка при добавлении записи? - PullRequest
0 голосов
/ 03 сентября 2018

У меня есть файл CSV, созданный программой, отличной от той, которую я пишу, поэтому я не могу изменить заголовки. Заголовки для файла ControllerID,Event,Number,ReaderID,Timestamp,TransactionID,. Я указал те же заголовки для CSVPrinter, который я использую для вставки строки в тот же CSV. Однако, когда принтер добавляет запись, он добавляет строку заголовка перед новой записью, как показано ниже:

0,1,2688229830,3,2018-09-03 13:54,63
ControllerID,Event,Number,ReaderID,Timestamp,TransactionID
0,1,4900920,2,2018-09-03,15:15,176492

Код выглядит следующим образом:

/* Add an entry to the TransactionLog CSV */
private static boolean logTransaction (
  int cid, int rid, boolean freeExit, long tsMS, long accNum, String reason
) {
  boolean added = false;
  int evt = freeExit ? 8 : 1;
  File transactLog = new File(Locations.getDatabaseDir(), "TransactLog.csv"),

  CSVFormat shortFormat = CSVFormat.DEFAULT.withQuote(null)
    .withAllowMissingColumnNames(true)/*.withTrailingDelimiter(true)*/
    .withHeader(Transaction.getHeader(false));
  // Attempt to read the last transaction ID and add 1 to it, to be used as ID of new transaction
  try (
    CSVParser shortParser = new CSVParser(new FileReader(transactLog), shortFormat);
    CSVPrinter shortWriter = new CSVPrinter(new FileWriter(transactLog, true), shortFormat);
  ) {
    // read in all the current records, getting the transactionID of the last record
    List<CSVRecord> shortRecords = shortParser.getRecords();
    int newTransId = 1;
    if (!shortRecords.isEmpty()) {
      CSVRecord last = shortRecords.get(shortRecords.size() - 1);
      String lastTransactionId = last.get("TransactionID");
      // add one to last transaction ID
      try {
        newTransId = Integer.parseInt(lastTransactionId) + 1;
      } catch (IllegalArgumentException IAEx) {
        newTransId = shortRecords.size() + 1;
        LOGGER.warn("Failed to determine last transaction ID from CSV ("
          + lastTransactionId + "); setting to recordCount (" + newTransId + ") ..."
        );
      }
    }
    // write the new transaction record
    Transaction t = new Transaction(cid, evt, accNum, rid, tsMS, newTransId, reason);
    List<String> tValues = t.asList(false), fValues = t.asList(true);
    shortWriter.printRecord(tValues);
    shortWriter.flush();
    added = true;
  } catch (IOException IOEx) {
    added = false;
    LOGGER.error(Util.getErrorTrace(
      "Failed to write transaction to \"" + transactLog.getAbsolutePath() + "\" CSV", IOEx
    ));
  }

  return added;
}

Код транзакции POJO опущен, так как он не имеет отношения к проблеме.

Как я могу предотвратить вывод CSVPrinter строки заголовка перед выводом строки? (Файл CSV уже содержит строку заголовка.)

1 Ответ

0 голосов
/ 03 сентября 2018

Чтобы не выводить строку заголовка, необходимо добавить .withSkipHeaderRecord(true) к экземпляру CSVFormat, используемому с CSVPrinter, как показано ниже:

CSVFormat skipFormat = CSVFormat.DEFAULT.withQuote(null)
  .withAllowMissingColumnNames(true).withTrailingDelimiter(true)
  .withHeader(Transaction.getHeader(false))
  .withSkipHeaderRecord(true).withIgnoreEmptyLines(true);

Звонить на .withIgnoreEmptyLines(true) необязательно. Это включено, чтобы не сохранять пустые / пустые строки.

Примечание: Если вы хотите вывести строку заголовка в качестве первой строки в файле, при его создании вы можете изменить .withSkipHeaderRecord(true) на .withSkipHeaderRecord(csvFile.exists()). Это работает, потому что файл не существует непосредственно перед созданием, но существует при следующей записи записи.

...