Update .csv file when edited in Java

0

I have a .csv file that contains information, but when I remove information (in this case contacts) from the console output the .csv file is not being updated. How to solve?

    private void insertContact(String contactName) {
        contactsListModel.addElement(new Contact(contactName));
    }

    private void setContactsList() {
        contactsListModel = new DefaultListModel<Contact>();
        contactsList = new JList<Contact>(contactsListModel);
contactsList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        contactsList.setLayoutOrientation(JList.VERTICAL);
        contactsList.setVisibleRowCount(-1);
        contactsList.setBackground(Color.LIGHT_GRAY);
        loadContacts();

        add(contactsList, BorderLayout.CENTER);     
    }


    private void setContactsLabel() {
        contactsLabel = new JLabel("Contacts:");
        contactsLabel.setOpaque(true);
        contactsLabel.setBackground(Color.WHITE);

        add(contactsLabel, BorderLayout.NORTH);
    }


        public void loadContacts(){

        BufferedReader br = null;
        String line = "";
        String separator = ";";

        try {

            br = new BufferedReader(new FileReader("diretorio"));
            while ((line = br.readLine()) != null) {
                String[] contactName = line.split(separator);

                contactsListModel.addElement(new Contact(contactName[0]));

                System.out.println( );

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
      }
}
    
asked by anonymous 04.11.2015 / 18:00

1 answer

2

You have created a method that reads the file and transforms it into a list of contacts. When you remove something from this list, you are removing only from the list. This is simply expected. You now need to make a method analogous to the one that loads the contacts to save the contacts from the list to a file and call it when you change the list. The easiest way is to make one that recreates the file (deleting what already exists) and printing there in the file one by one of the contacts. It is important to keep the same format you use for reading.

    
04.11.2015 / 19:33