本文介绍了SuperCSV追加而不是覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能新行添加到CSV文件,而不是覆盖最后一个?

Is it possible to add new lines to a CSV file, rather than overwrite the last one?

下面是我的方法,我打电话时,我想添加一个新行:

Here is my method I call when I want to add a new line:

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();
        }
    }
}

感谢

推荐答案

添加真正新的FileWriter参数

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

这篇关于SuperCSV追加而不是覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 07:58