Tell me how to implement. At the moment there is a method for adding a new entry in CSV:

private static void addContact() { String outputFile = "myPhoneBook.csv"; boolean alreadyExists = new File(outputFile).exists(); try { CsvWriter csvOutput = new CsvWriter(new FileWriter(outputFile, true), ','); if (!alreadyExists) { csvOutput.write("ID"); csvOutput.write("Name"); csvOutput.write("Phone"); csvOutput.write("City"); csvOutput.endRecord(); } csvOutput.write(new String(String.valueOf(getLastColumnValue()))); csvOutput.write("Bruce"); csvOutput.write("12345"); csvOutput.write("Plovdiv"); csvOutput.endRecord(); csvOutput.close(); } catch (IOException e) { e.printStackTrace(); } } 

According to the task there is no possibility to insert duplicate values ​​for the name and phone number. Tell me how you can implement.

  • Take any in-memory database, and in CSV just merge data from there as in persistent storage. - enzo

1 answer 1

Do you have any training assignment? Verifying the uniqueness of a record works well in databases or at least in collections. Checking the uniqueness directly in the CSV file each time before inserting a new record is very inefficient in terms of performance, speed of the program. Each time before adding a new record you need to read the contents of the file from the beginning to the end, comparing each read line with your new record and making sure that there is no similar record in the file, add a new record to the end of the CSV file or not add one if it already exists.