写入我的CSV文件会删除现有数据,如何解决此问题?

时间:2017-07-25 12:09:18

标签: java excel eclipse csv filewriter

我正在编写一个程序,要求我写入Excel文件。我有代码允许我这样做,但是,如果我尝试向文件添加更多数据,它会删除现有数据并替换它。我该如何解决?我的代码如下所示:

JButton addClientButton = new JButton("Add");
        addClientButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                PrintWriter pw = null;
                Client newClient = new Client(firstNameTextField.getText(), lastNameTextField.getText(), emailTextField.getText(), phoneTextField.getText(), weightTextField.getText(), heightTextField.getText(), ageSpinner.getValue(), activityLevelComboBox.getSelectedItem());
                try {
                    pw = new PrintWriter(new File("Clients.csv"));
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                StringBuilder sb = new StringBuilder();
                sb.append(newClient.getFirst());
                sb.append(",");
                sb.append(newClient.getLast());
                sb.append(",");
                sb.append(newClient.getEmail());
                sb.append(",");
                sb.append(newClient.getPhone());
                sb.append(",");
                sb.append(newClient.getWeight());
                sb.append(",");
                sb.append(newClient.getHeight());
                sb.append(",");
                sb.append(newClient.getClientAge());
                sb.append(",");
                sb.append(newClient.getClientActivity());
                sb.append("\n");

                pw.write(sb.toString());
                pw.close();
            }
        });

0 个答案:

没有答案