【问题标题】:How to write in specific cell in a CSV file?如何在 CSV 文件的特定单元格中写入?
【发布时间】:2019-10-30 10:51:02
【问题描述】:

我需要在 .csv 文件中的特定单元格中写入 org.apache.commons.csv 库,而不会迭代并复制所有行。

我试过了:

Reader reader = Files.newBufferedReader(Paths.get(Properties.dataFilePath));
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT);
List<CSVRecord> records = csvParser.getRecords();
records.get(rowNumber)[columnIndex] = value;

但这是不允许的。

编辑 1

我的 IDE 提到:The type of the expression must be an array type but it resolved to CSVRecord

【问题讨论】:

  • 你能分享一下具体的错误信息吗?
  • 我用消息更新了我的问题。

标签: java csv apache-commons-csv


【解决方案1】:

你试过了吗 value = records.get(rowNumber).get(columnIndex);?

看看这里,可能对你有帮助:https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVRecord.html

【讨论】:

  • 是的,但我无法使用get 方法设置某些内容。
  • 对不起,我没听懂你的第一次尝试不正确。
  • 您不能覆盖 CVSRecord 中的值,它仅用于读取。如果要更改 CSV 文件的内容,则必须使用 CSVRecoed 读取文件的内容,对其进行修改,然后必须使用 CSVPrinter 将其写入新文件。阅读:commons.apache.org/proper/commons-csv/… 打印:commons.apache.org/proper/commons-csv/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多