问题
I need to update a row with new values using openCSV utility. Say for example, I have below CSV file, I need to update the rows containing TestdminUsername1
and TestdminUsername2
to TestdminUsername1,Fail,Errors Found
and TestdminUsername2,Fail,Errors Found
respectively.
TestdminUsername1,Pass,No Error
TestAdminUsername2,Pass,No Error
TestAdminUsername3,Pass,No Error
TestAdminUsername4,Pass,No Error
TestAdminUsername5,Pass,No Error
TestAdminUsername6,Pass,No Error
TestAdminUsername7,Pass,No Error
TestAdminUsername8,Pass,No Error
TestAdminUsername9,Pass,No Error
TestAdminUsername10,Pass,No Error
Below is the piece of code am using :
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Arrays;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
public class AppendToCSVExample
{
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception
{
String csv = "D:\\data2.csv";
CSVWriter writer = new CSVWriter(new FileWriter(csv, true));
String [] record1 = "TestdminUsername1,Pass,No Error".split(",");
String [] record2 = "TestAdminUsername2,Pass,No Error".split(",");
String [] record3 = "TestAdminUsername3,Pass,No Error".split(",");
String [] record4 = "TestAdminUsername4,Pass,No Error".split(",");
String [] record5 = "TestAdminUsername5,Pass,No Error".split(",");
String [] record6 = "TestAdminUsername6,Pass,No Error".split(",");
String [] record7 = "TestAdminUsername7,Pass,No Error".split(",");
String [] record8 = "TestAdminUsername8,Pass,No Error".split(",");
String [] record9 = "TestAdminUsername9,Pass,No Error".split(",");
String [] record10 = "TestAdminUsername10,Pass,No Error".split(",");
writer.writeNext(record1);
writer.writeNext(record2);
writer.writeNext(record3);
writer.writeNext(record4);
writer.writeNext(record5);
writer.writeNext(record6);
writer.writeNext(record7);
writer.writeNext(record8);
writer.writeNext(record9);
writer.writeNext(record10);
writer.close();
CSVReader reader = new CSVReader(new FileReader("D:\\data1.csv"), ',' , '"' , 1);
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
if (nextLine != null) {
//Verifying the read data here
System.out.println(Arrays.toString(nextLine));
String[] parts = Arrays.toString(nextLine).split(",");
if (parts[0].toString().contains("TestdminUsername1")) {
String [] recordTest = "TestdminUsername1, Fail, Errors found".split(",");
//Which method in openCSV needs to be used to update recordTest value in the File data2.csv
;
}
}
}
}
}
回答1:
Personally I would recommend that you open a second CSVWriter to a new file (data3.csv) and as you read from data1 you write to data3 changing what data needs to be modified. This way if you have a failure for whatever reason you still have your original file. And at the end if you want only one file you can delete data1 and rename data3.csv to data1.csv.
There was a previous question where someone was trying to read and write into the same file at the same time (Reading from a CSV file and writing to a new CSV file using openCSV in java) but there again I would advise against that in case of an error.
来源:https://stackoverflow.com/questions/31380618/update-a-row-with-new-values-using-opencsv