通过 opencsv 在文件末尾写入
我正在使用 opencsv 并希望通过多个会话写入 .csv
文件。 但是,每次我启动新的 CSVWriter 时,旧文件都会被删除。 我可以更改 CSVWriter 的行为以在末尾写入吗文件而不是替换文件?
I'm using opencsv and want to write to a .csv
file through multiple sessions.
However every time I start a new CSVWriter the old file gets erased.
Can I change the behavior of the CSVWriter to write at the end of the file instead of replacing the file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
FileWriter 中有一个选项,而不是CSVWriter 添加到文件末尾。
这段代码使它工作:
There's an option in the FileWriter instead of the CSVWriter to append at the end of the file.
This code makes it work:
这可以通过 OpenCSV 实现,请查看下面的示例,将结果集附加到现有的 csv 文件。
writer.writeAll(结果集, true);
FileWriter 构造函数的第二个参数是 bool,表示以追加模式打开文件。
This is possible with OpenCSV, please have a look at the below example to append resultset to the existing csv file.
writer.writeAll(resultSet, true);
The second parameter to the FileWriter constructor is bool to open a file in append mode.
似乎不可能附加到 opencsv 中的文件(从最初的外观来看,它看起来相当简单),但如果您不限于 opencsv,您可以尝试 JExcel.要附加到 JExcel 中的文件,您本质上需要创建一个副本,然后处理该副本,并覆盖原始副本。 OpenCSV 中的情况也类似。
编辑:看来您唯一真正的选择是尝试 JExcel 或将整个文件读入列表,附加到它,然后写出来。如果这对内存来说太重,请保持流打开,读入块,写出块,然后写出附加块。
It doesn't seem possible to append to a file in opencsv (from an initial look, it looks rather simple), but if you're not restricted to opencsv you can try JExcel. To append to a file in JExcel you essentially need to create a copy then work off that, and overwrite the original. That could be similar in OpenCSV.
Edit: It seems like your only real option is to try out JExcel or read the entire file into a list, append to it, and write that out. If this is too heavy on memory, keep the stream open, read in chunks, write out chunks, then write out your appended chunk.
应该是可能的:
来自 H2 数据库的 CSV 工具 (免责声明:我写的)也支持这一点。
It should be possible:
The CSV tool from the H2 database (disclaimer: I wrote it) also supports this.