Java append new column to csv file

不想你离开。 提交于 2019-11-27 17:03: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 column.

Here is what I did:

try {
    FileWriter writer = new FileWriter(OUT_FILE_PATH, true);
    for (int i=0; i<data.size(); i++) {
        writer.append(String.valueOf(data.get(i)));
        writer.append(",");
        writer.append("\n");
    }
    writer.flush();
    writer.close();
} catch (Exception e) {} 

Result - It appends the new column below the first column, so I have single long column.

Thanks,


回答1:


You will have to read your file (line by line) and then insert the new column to every line. Here's a solution using BufferedReader and BufferedWriter

public void addColumn(String path,String fileName) throws IOException{
    BufferedReader br=null;
    BufferedWriter bw=null;
    final String lineSep=System.getProperty("line.separator");

    try {
        File file = new File(path, fileName);
        File file2 = new File(path, fileName+".1");//so the
                    //names don't conflict or just use different folders

        br = new BufferedReader(new InputStreamReader(new FileInputStream(file))) ;
        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file2)));
        String line = null;
                    int i=0;
        for ( line = br.readLine(); line != null; line = br.readLine(),i++)
        {               

            String addedColumn = String.valueOf(data.get(i));
            bw.write(line+addedColumn+lineSep);
    }

    }catch(Exception e){
        System.out.println(e);
    }finally  {
        if(br!=null)
            br.close();
        if(bw!=null)
            bw.close();
    }

}



回答2:


Something like this perhaps:

public void appendCol(String fileName, ???ArrayList??? data) { //assuming data is of type ArrayList here, you need to be more explicit when posting code

    String lineSep = System.getProperty("line.separator");
    String output = "";
    try{
        BufferedReader br = new BufferedReader(new  FileReader(fileName));
        String line = null;
        int i = 0;
        while ((line = br.readLine()) != null) {
            output += line.replace(
                    lineSep,
                    "," + String.valueOf(data.get(i)) + lineSep);
            i++;
        }
        br.close();
        FileWriter fw = new FileWriter(fileName, false); //false to replace file contents, your code has true for append to file contents
        fw.write(output);
        fw.flush();
        fw.close();
    } catch (Exception e){
        e.printStackTrace();
    } 
}



回答3:


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


来源:https://stackoverflow.com/questions/34590971/java-append-new-column-to-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!