11

I have a incomplete csv file that needs to be accurately updated, so there is csv file like this :

one,two,three,four,five,six,seven,eight,nine,ten //This is a line(String)

Naturally the file is far more complex but in this format, here is what I want to do insert new-word,new-word1, new-word3 or n words between seven and eight(or any range). How can I do that?

Pseudo-code,code or example would be great, I don't have a clue how to even start.

UPDATE:

Maybe I should convert this to array or some kind of datastructure. Then insert new item at the certain position, shift the rest of the content right and do that for each insertion.

I don't know if is right way to go or how to begin programming it

UPDATE II:

Maybe read in csv in the list, split list into two lists, first one ending with seven. Then add n words to first list and join two lists at the end? also don't know how to program this

Gandalf StormCrow
  • 25,788
  • 70
  • 174
  • 263

3 Answers3

7

Take a look at OpenCSV.

UPDATE: Here's some (barely) pseudocode to give a general idea of how you would use OpenCSV for this:

CSVReader reader = new CSVReader(new FileReader("old.csv"));
CSVWriter writer = new CSVWriter(new FileWriter("new.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
    List<String> lineAsList = new ArrayList<String>(Arrays.asList(nextLine));
    // Add stuff using linesAsList.add(index, newValue) as many times as you need.
    writer.writeNext(lineAsList.toArray());
}

Hat-tip: @Mark Peters who points out that you can't update the results of Arrays.asList

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
  • @Hank Gay I either don't understand what you've posted or this writes your existing entries to a new file, I don't see anywhere here how can I write between items – Gandalf StormCrow Jun 24 '10 at 14:30
  • Will OpenCSV escape double quotes properly? – Joe Phillips Jun 24 '10 at 14:47
  • @Joe Phillips I guess that depends on your definition of properly, since there's no spec. It's a straightforward lib; I recommend you experiment with it and read the [FAQ on its homepage](http://opencsv.sourceforge.net/). – Hank Gay Jun 24 '10 at 14:56
  • 2
    Just want to note that Arrays.asList is an unmodifiable collection. You want `new ArrayList(Arrays.asList(nextLine))` – Mark Peters Jun 24 '10 at 14:56
  • Last time I used this library it was memory consuming (If you have to process file ~15 gb size it is very important to not require a lot of ram; I had to optimize it in OpenCSV) – uthark Jun 24 '10 at 17:05
  • @uthark Even when operating on one line at a time (as in the example)? I would expect the `readAll` functionality to consume lots of RAM on big files, but line-at-a-time should be very efficient, because it doesn't need to store more than a single line. – Hank Gay Jun 24 '10 at 18:56
1

This pseudo-like code might help :

List values = Arrays.asList(line.split(",\s*"));
List newWords = Arrays.aList(newWordsLine.split(",\s*"));

values.addAll(7,newWords);

StringBuffer buf = new StringBuffer(values.get(0));
for(v : values.subList(1,values.size()) {
    buf.append(",",v);
}
return buf.toString();

this will insert the newWords after the 7th item on the line

Peter Tillemans
  • 34,983
  • 11
  • 83
  • 114
0

I'd parse it into a collection, add your items, then render it back out to a csv.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143