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

时间:2016-06-17 08:37:02

标签: java serialization deserialization

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

我想使用ObjectInputStreamObjectOutputStream来编写和阅读文本文件。使用此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之后我输入23,但输出test.ser写只有值3,当我阅读test.ser文件,然后在动态tf上,我得到了值33

我想问一下如何编写和读取动态添加的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)

1 个答案:

答案 0 :(得分:2)

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

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

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