Java append new column to csv file

后端 未结 3 987
旧时难觅i
旧时难觅i 2020-12-16 08:24

I want to calculate some column data and write it to csv file as column. Then after calculating other column of data I want to append it to same file but as new

3条回答
  •  北海茫月
    2020-12-16 09:19

    Hope this will help you.

    {
        //CREATE CSV FILE 
        StringBuffer csvReport = new StringBuffer(); 
        csvReport.append("header1,Header2,Header3\n"); 
        csvReport.append(value1 + "," + value2 + "," + value3 + "\n"); 
    
        generateCSVFile( filepath,fileName, csvReport); // Call the implemented mathod 
    }
    
    public void generateCSVFile(String filepath,String fileName,StringBuffer result)
    {
        try{
    
        FileOutputStream fop = new FileOutputStream(filepath);
    
        // get the content in bytes
        byte[] contentInBytes = result.toString().getBytes();
    
        fop.write(contentInBytes);
        fop.flush();
    
        //wb.write(fileOut);
        if(fop != null)
            fop.close();
        }catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    

提交回复
热议问题