Update. csv file when edited in Java

Asked

Viewed 259 times

0

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

    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();
                }
            }
        }
      }
}
  • What is contactsListModel? Your code seems to be incomplete.

  • Private defaultlistmodel<Contact> contactsListModel;

  • But where did you come from?

  • now you see??

  • Good afternoon, note that you have only posted part of the code, so much so that the Buttonlistener class is not entire, the }. Follow these tips from http://answall.com/help/mcve. - I’m sure you’ll take my comment as a constructive criticism.

  • this buttonlistenner class is whole yes, now if it is correct I don’t know. thanks! can help me with the question?

Show 1 more comment

1 answer

2

You created a method that reads the file and turns it into a contact list. When you remove something from that list, you are only removing it from the list. This is simply expected. Now you need to do a method analogous to that which loads the contacts from the list to save the contacts from the list in a file and call it when you change the list. The easiest way is to make one that recreates the file (erasing 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.

  • How is the format of your file?

  • the format is in one column the name in the other front column the number (file . csv)

  • how do I do it in code? can you give me a help?

  • Blz. So basically you create a Bufferedwriter by passing the address of your file as parameter and write a string containing the contact name, comma and number for each element of the list using a for. The Bufferedwriter is almost identical to the Bufferedreader, so no mistake.

  • I’ll update when I get home.

  • thanks a lot for the help I’ll be waiting, until now

  • Can you help me?

  • I’m here. Please chat.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.