I have gone through other questions regarding this on StackOverflow but libraries mentioned there only allow to read or write. Some of them do allow partial read or write like SuperCSV but no library can be used for updating value of a particular cell. Is there some other library that can be used or do I need to do it manually?
Asked
Active
Viewed 2,500 times
3 Answers
1
Read in the CSV, modify the cell you want, then write it back to the file.

tskuzzy
- 35,812
- 14
- 73
- 140
-
How do I modify the cell? Actually this capability will be used in a Neural Engine. Its not like I want to do this for a particular file. – rishiag Jun 14 '12 at 14:53
1
No, there is no way to directly update the cell in CSV file. You can read CSV line by line and then column by column , update the cell (content) and write to a new file.
And if you know the pattern(regex) then you can use String.replace() method.

amicngh
- 7,831
- 3
- 35
- 54
-
Yup, that's what I read at another question on SO. Could you also tell me what is the best way to update the content of the cell? Should I parse them into string or should I use a library? – rishiag Jun 14 '12 at 14:59
-
i dont know any library better to read the file in string and replace the content. – amicngh Jun 14 '12 at 15:04
0
There is no in build function to edit csv file. Here is one approach to edit csv file.
package maven.selenium;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
import jxl.read.biff.BiffException;
import jxl.read.biff.File;
public class App {
public static void main(String[] args) throws BiffException, IOException {
String csv = "C:/Users/shubham.mahajan/Desktop/data.csv";
CSVReader reader = new CSVReader(new FileReader(csv), ',');
List<String[]> csvBody = reader.readAll();
String[] First_Row = csvBody.get(0);
CSVWriter writer = new CSVWriter(new FileWriter(csv), ',');
writer.writeNext(First_Row);
for (int k = 0; k < First_Row.length; k++) {
if (First_Row[k].equals("value")) {
for (int j = 1; j < csvBody.size(); j++) {
String[] Temp = csvBody.get(j);
Temp[k] = "replacing value";
writer.writeNext(Temp);
}
}
}
reader.close();
writer.flush();
writer.close();
}
}

shubham mahajan
- 1
- 1
-
1Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 21 '21 at 05:10
-
Please add additional details to the [answer](https://stackoverflow.com/help/how-to-answer) provided . – George Oct 21 '21 at 05:23