Java:导入保存的ArrayList并显示在文本字段中
遇到这个问题。
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论