有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

数组Java从csv文件获取id到组合框

我有一个计划,有两种形式,一种是给顾问的,另一种是给客户的。在第一个表单中,用户将输入咨询师的详细信息,他的ID将保存在csv文件中,这可以正常工作

Consultant cons_save = new Consultant();
    cons_save.setPersonfirstname(this.jTextField1.getText());
    cons_save.setPersonlastname(this.jTextField2.getText());
    cons_save.setPersonID(this.jTextField4.getText());
    this.jTextField1.setText("");
    this.jTextField2.setText("");
    this.jTextField3.setText("");

    cons_save.ConsultantID = cons_save.PersonID;

    cons_save.setConsultantID(this.jTextField4.getText());
    this.jTextField4.setText("");

    try
{
        BufferedWriter writer = new BufferedWriter(new FileWriter("E:\\ryan_assignment_sit2\\ConsID\\consID.csv", true));

    writer.append(cons_save.ConsultantID);  
        writer.append(",");
    writer.flush();
    writer.close();
}
catch(IOException e)
{
     e.printStackTrace();
} 

    cons_save.savecons();  

保存id后,将id从csv调出到一个数组,这也可以正常工作

       public CreateCustomer() {
    initComponents();

    ArrayList<String> ConsIDList = new ArrayList<String>();

    String csvFileToRead = "E:\\ryan_assignment_sit2\\ConsID\\consID.csv"; // Reads the CSV File.
    BufferedReader br = null; // Creates a buffer reader.
    String line = "";
    String splitBy = ","; // Reader Delimiter

    try {
        br = new BufferedReader(new FileReader(csvFileToRead)); // Buffer Reader with file name to read.
        Scanner reader = new Scanner(System.in);
        while ((line = br.readLine()) != null) { //While there is a line to read.
            reader = new Scanner(line);
            reader.useDelimiter(splitBy);

            while (reader.hasNext()) { // While there is a next value (token).
                ConsIDList.add(reader.next());
            }
        }

    } catch (FileNotFoundException exception) { // Exception Handler if the File is not Found.
        exception.printStackTrace();
    } catch (IOException exception) { // Input/Output exception
        exception.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close(); // Close the Scanner.
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }



        Vector<String> vectorData = new Vector<String>(ConsIDList);

        DefaultComboBoxModel<String> comboBoxModel = new DefaultComboBoxModel<>(vectorData);

        this.jComboBox1.setModel(comboBoxModel);



    }
}

数组工作正常,但组合框中没有填充arraylist


共 (0) 个答案