0

I need to update an existing entry in a CSV file using java code. The code that I have written is below. I'm able to match the entry given by the user with the entry in the file, but can't figure how to write a new entry at the same location.

while ((row = reader.readNext()) != null)       
{     
    for (int i = 0; i < 1; i++)                
    {    
        System.out.print("row is "+row[i]);

        // display CSV values                                                
        System.out.println("Cell Value: " + row[i]);  
        System.out.println("User Input: " + t1);
        System.out.println("-------------"); 
        if(t1.equals(row[0]))
        {
            data.add(new String[] { t1, t2, t3, t4, t5, t6, t7, t8, t9, t10,
                     t11, t12, t13, t14, t15, t16, t17, t18, t19, t20, t21, t22,
                     t23, t24, t26, t27, t28, t29, t30, t31, t32, t33, t34, t35,
                     t36, t37, t38, t39 
            });   
            flag=1;
            writer.writeAll(data);
            break;
        }               
    }
    rowno++;
}

1 Answers1

0

This is what you need to do to write a new value to the same location

  • Have two Files, The input one to read from, and new one to write to
  • In the loop, read a line from file, and if it doesn't match, write to output file
  • If found from input file, write new entry in output file.
  • Finish looping through file.
  • Close the Streams
  • Change the name of the output file to the name of the input file.
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720