5

It there any possibility to add a new column to existing csv file, means

existing file

userid,name
1,Jim
2,Sally
3,Bob

modified output file

userid,name,time
1,Jim,64913824823208
2,Sally,64913824900056
3,Bob,64913824966956

Thanks in advance

1 Answers1

4

You mean something like this?

CSVReader reader = new CSVReader(new FileReader("input.csv"));
CSVWriter writer = new CSVWriter(new FileWriter("output.csv"), ',');
String[] entries = null;
while ((entries = reader.readNext()) != null) {
    ArrayList list = new ArrayList(Arrays.asList(entries));
    list.add(xxx); // Add the new element here
    writer.writeNext(list);
}
writer.close();

Haven't tried to compile it but something like this should work. There are many options to use OpenCsv (e.g., you can link the data to beans directly), but this is the shorter solution.

rlegendi
  • 10,466
  • 3
  • 38
  • 50
  • i expected something that 'll allow me to write in the same input file itself, after the research we came to know it is less possible, so I accepting your answer, thanks for your time – Suganthan Madhavan Pillai Oct 21 '14 at 04:20
  • 1
    See http://stackoverflow.com/questions/12713247/reading-and-writing-from-and-to-the-same-file-in-java – rlegendi Oct 21 '14 at 06:12