Java:导入保存的ArrayList并显示在文本字段中

发布于 2024-12-05 00:34:37 字数 1531 浏览 1 评论 0原文

遇到这个问题。

我有一个 ArrayList,我已将其输出到一个文件,我希望能够打开该文件并在 jTextField 中显示内容

private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {                                          
   computer= new Computer();
   computer.setComputerType(cbType.getSelectedItem().toString() +"\n");
   computer.setWeight(txtWeight.getText() +"\n");
   computer.setBatteryLife(txtBattery.getText() +"\n");
   computer.setScreenSize(txtScreenSize.getText() +"\n");
   computer.setRamSize(txtRAM.getText() +"\n");
   computer.setOS(cbOS.getSelectedItem().toString() +"\n");
   computer.setManufacturerName(txtManufacturer.getText() +"\n");
   computer.setProcessorModel(txtCPU.getText() +"\n");
   computer.setCpuCoreType(txtCPUCore.getText() +"\n");

    //Store computer in ArrayList
    myComputerList.add(computer);

    System.out.print("Size of array " + myComputerList.size() + "\n");
     System.out.print("\n");
}

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        

    try {
        ObjectOutputStream outputStream = new ObjectOutputStream(
        new FileOutputStream("ArrayFile.bin"));
        outputStream.writeObject(myComputerList);
        outputStream.close();
        System.out.println("Array output file" + "\n");
        }
            catch(IOException err)
            {
                System.out.println("Problem with file output");
            }
            System.out.println("Computer written to the file computers.bin");
} 

having trouble with this.

I have an ArrayList which I have output to a file, I want to be able to open this file and display the contents in a jTextField

private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {                                          
   computer= new Computer();
   computer.setComputerType(cbType.getSelectedItem().toString() +"\n");
   computer.setWeight(txtWeight.getText() +"\n");
   computer.setBatteryLife(txtBattery.getText() +"\n");
   computer.setScreenSize(txtScreenSize.getText() +"\n");
   computer.setRamSize(txtRAM.getText() +"\n");
   computer.setOS(cbOS.getSelectedItem().toString() +"\n");
   computer.setManufacturerName(txtManufacturer.getText() +"\n");
   computer.setProcessorModel(txtCPU.getText() +"\n");
   computer.setCpuCoreType(txtCPUCore.getText() +"\n");

    //Store computer in ArrayList
    myComputerList.add(computer);

    System.out.print("Size of array " + myComputerList.size() + "\n");
     System.out.print("\n");
}

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        

    try {
        ObjectOutputStream outputStream = new ObjectOutputStream(
        new FileOutputStream("ArrayFile.bin"));
        outputStream.writeObject(myComputerList);
        outputStream.close();
        System.out.println("Array output file" + "\n");
        }
            catch(IOException err)
            {
                System.out.println("Problem with file output");
            }
            System.out.println("Computer written to the file computers.bin");
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文