更新的代码显示为://1
,//2
,//3
,//4
,//5
。
我想使用ObjectInputStream
和ObjectOutputStream
来编写和阅读文本文件。使用此tutorial我尝试将其实现到我的班级:
//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
值?
输出我收到此错误:
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)
答案 0 :(得分:2)
如果我正确理解了您的问题,那么您将在循环的每个e.name2
中覆盖存储在iteration
中的先前值。
因此,当您反序列化时,您将只获得归档e.name2
的最后写入值。
要保留多个值,请使用ArrayList<String>
代替e.name2
。