tags:

views:

96

answers:

3

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

+2  A: 

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
@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
Will OpenCSV escape double quotes properly?
Joe Philllips
@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
Just want to note that Arrays.asList is an unmodifiable collection. You want `new ArrayList(Arrays.asList(nextLine))`
Mark Peters
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
@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
A: 

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

Marcus Adams
@Marcus Adams yes, how would you insert n items to any collection ex:array list between two items which are already inside
Gandalf StormCrow
You'd find the object before you wanted to insert, get it's index, and then insert the three things right there?
Dean J
For ArrayList, use the `add(int, object)` method.
Marcus Adams
@Dean J how would I do that?
Gandalf StormCrow
+1  A: 

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