user2142786
user2142786

Reputation: 1482

Update a specific row in CSV file

I want to update a specific row (that satisfies a pre-defined condition) in a .CSV file.

This is my code:

ProductID,ProductName,price,availability,type
12345,abc,300,yes,medicine
23456,def,400,yes,testing
34567,ghi,200,no,medicine
45678,jkl,500,no,testing

here is code

    CsvReader products = new CsvReader("D:\\sample.csv");
            CsvWriter csvOutput = new CsvWriter(new FileWriter("D:\\sample.csv", true), ',');
            products.readHeaders();
            products.getRawRecord();
            while (products.readRecord())
            {
                String productID = products.get("ProductID");
                String productName = products.get("ProductName");
                String supplierID = products.get("price");
                String categoryID = products.get("availability");
                String quantityPerUnit = products.get("type");

                if(productID.equals("roche123")){
                    csvOutput.replace(productName, "12", "newproductName");
                    csvOutput.replace(supplierID , "12", "newsupplierID ");
                }

But it doesn't work. Can anyone help?

Upvotes: 2

Views: 5098

Answers (1)

SubOptimal
SubOptimal

Reputation: 22973

The method CsvWriter.replace is not an instance method and does not an inline replacement on the current record.

Rather it should be used as

String replace = CsvWriter.replace(
    inputString, // somefoobar
    substringToReplace, // foobar
    substringSubstitute); // candy
// replace will be "somecandy"

Following snippet should demonstrate the principle.

assuming sample_in.csv as

ProductID,ProductName,price,availability,type
12345,abc,300,yes,medicine
23456,def,400,yes,testing
34567,ghi,200,no,medicine
45678,jkl,500,no,testing
roche123,product 12,supplier 12,no,foobar

the snippet

CsvReader products = new CsvReader("sample_in.csv");
CsvWriter csvOutput = new CsvWriter(new FileWriter("sample_out.csv", true), ',');
products.readHeaders();

csvOutput.writeRecord(products.getHeaders());

String[] outValues = new String[5];
while (products.readRecord()) {
    String productID = products.get("ProductID");
    String productName = products.get("ProductName");
    String supplierID = products.get("price"); // is this correct?
    String categoryID = products.get("availability");
    String quantityPerUnit = products.get("type");

    if (productID.equals("roche123")) {
        productName = CsvWriter.replace(productName, "12", "newproductName");
        supplierID = CsvWriter.replace(supplierID, "12", "newsupplierID");
    }

    outValues[0] = productID;
    outValues[1] = productName;
    outValues[2] = supplierID;
    outValues[3] = categoryID;
    outValues[4] = quantityPerUnit;
    csvOutput.writeRecord(outValues);
}

will produce sample_out.csv as

ProductID,ProductName,price,availability,type
12345,abc,300,yes,medicine
23456,def,400,yes,testing
34567,ghi,200,no,medicine
45678,jkl,500,no,testing
roche123,product newproductName,supplier newsupplierID,no,foobar

from the input line

roche123,product 12,supplier 12,no,foobar
  • column ProductName is updated from product 12 to product newproductName
  • column price is updated from supplier 12 to supplier newsupplierID

edit The snippet is using the JavaCSV library.

Upvotes: 2

Related Questions