gpt4 book ai didi

java - 动态添加文本字段值的序列化

转载 作者:行者123 更新时间:2023-12-02 03:32:36 25 4
gpt4 key购买 nike

更新后的代码显示为://1 , //2 , //3 , //4 , //5 .

我想使用ObjectInputStreamObjectOutputStream写入和读取文本文件。使用这个tutorial我尝试将其实现到我的类(class)中:

       //Serializable Employee class
public class Employee implements java.io.Serializable
{
public String name;
public String name2;

public ArrayList <String> arr; //1
}
.....

//ObjectOutputStream class

Employee e = new Employee();
e.name = jTextField1.getText();

for (int i = 0; i < jPanel1.getComponentCount(); i++) {
SubPanel panel = (SubPanel) jPanel1.getComponent(i);
JTextField tf = panel.getTf();

e.arr.add(tf.getText()); //2
String b = e.arr.get(i); //3
System.out.println(b); //4
}

try
{
FileOutputStream fileOut = new FileOutputStream("test.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(arr);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in test.ser");
}catch(IOException i)
{
i.printStackTrace();
}
.....

//ObjectInputStream class
Employee e = null;
try
{
FileInputStream fileIn = new FileInputStream("test.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
jTextField1.setText(e.name);

for (int i = 0; i < jPanel1.getComponentCount(); i++) {
SubPanel panel = (SubPanel) jPanel1.getComponent(i);
JTextField tf = panel.getTf();

tf.setText(e.arr.get(i)); //5
}
}

但是在 ObjectOutputStream class在这个领域e.name2 = tf.getText();如果添加2个动态后tf我输入2 , 3 ,但输出 test.ser仅写入值 3 ,当我读到test.ser时文件,然后动态 tf我得到的值 3 , 3 .

请问如何动态添加tf写入和读取值(value)观?

输出我收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Test2.Test.serActionPerformed(Test.java:2331)
at Test2.Test.access$800(Test.java:47)
at Test2.Test$9.actionPerformed(Test.java:591)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

最佳答案

如果我正确理解你的问题,那么你将覆盖 e.name2 中存储的先前值。在每个 iteration循环的。

因此,当您反序列化时,您将仅获得归档 e.name2 的最后写入值。 .

要保存多个值,请使用 ArrayList<String>代替e.name2 .

关于java - 动态添加文本字段值的序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37876839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com