gpt4 book ai didi

java - SuperCSV 附加而不是覆盖?

转载 作者:太空狗 更新时间:2023-10-29 16:42:19 24 4
gpt4 key购买 nike

是否可以向 CSV 文件添加新行,而不是覆盖最后一行?

这是我想添加新行时调用的方法:

private static void writeWithCsvMapWriter() throws Exception 
{
final String[] header = new String[] { "Engineer", "Time/Date", "Project", "Test ID", "Data Centre", "Data Hall", "Row", "Grille", "I/S" };

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd_HH:mm:ss");
String currentDateandTime = sdf.format(new Date());

final Map<String, Object> NewRow = new HashMap<String, Object>();
NewRow.put(header[0], Name.getText().toString());
NewRow.put(header[1], currentDateandTime);
NewRow.put(header[2], "Expect");
NewRow.put(header[3], TestID.getText());
NewRow.put(header[4], DataCentre.getText());
NewRow.put(header[5], DataHall.getText());
NewRow.put(header[6], Row.getText());
NewRow.put(header[7], Grille.getText());
NewRow.put(header[8], IS.getText());

ICsvMapWriter mapWriter = null;

try
{
mapWriter = new CsvMapWriter(new FileWriter("data/data/" + PACKAGE_NAME +"/writeWithCsvMapWriter.csv"), CsvPreference.STANDARD_PREFERENCE);

final CellProcessor[] processors = getProcessors();

mapWriter.writeHeader(header);
mapWriter.write(NewRow, header, processors);
}
finally
{
if( mapWriter != null )
{
mapWriter.close();
}
}
}

谢谢

最佳答案

new FileWriter的参数中加入true:

mapWriter = new CsvMapWriter(new FileWriter(".../writeWithCsvMapWriter.csv", true), CsvPreference.STANDARD_PREFERENCE);

关于java - SuperCSV 附加而不是覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15729977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com