反序列化后,Object ArrayList会丢失String值吗?

时间:2016-04-08 22:42:42

标签: java serialization arraylist

所以我有一个三级视频商店GUI,它应该保存库存视频的记录。然而,它使用序列化保存视频对象但由于某种原因,即使我没有得到任何错误,只有数值正在通过..

enter image description here

注意最左边的三列和最右边的列是如何都是空的。这是因为它们意味着在它们中有字符串,但它们却不...

正如我所说,我没有收到任何错误,所以这真让我困惑。

VideoStore.java的构造函数(GUI类):

public VideoStore() {
        initComponents();
        model = (DefaultTableModel)displayVideos.getModel();
        try{
        BinaryFile = new BinaryFile();
        BinaryFile.load();
        }
        catch(Exception e){

        }
            for(int j = 1; j < BinaryFile.videosList.size(); j ++) {
            Video load = (Video)BinaryFile.videosList.get(j);
            String tempName = load.getVideoName();
            String tempProd = load.getProducer();
            String tempRat = load.getRating();
            String tempGenre = load.getGenre();
            short tempNum = load.getVidNum();
            float tempPrice = load.getvideoPrice(); 

            try {
                Object[] row = {ID, tempName, tempProd, tempGenre, tempPrice, tempNum, tempRat};
                model.addRow(row);
            } catch(Exception e){

            }

            ID++;
        }
    }

然后我用来处理.ser文件的BinaryFile类:

public void load(){
        try
      {
         FileInputStream fileIn = new FileInputStream("/Users/hanaezz/Desktop/output.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         videosList = (ArrayList)in.readObject();
         in.close();
         fileIn.close();
      } catch(Exception i)
      {
         i.printStackTrace();
         return;
      }
    }

    public static void adderoo(Video v) {
        videosList.add(v);
    }

最后,ArrayList中的视频类:

private static String videoName;
private static String producer;
private static String rating;
private static String genre;
private short videoNumber;
private float videoPrice;

1 个答案:

答案 0 :(得分:6)

静态变量是 NOT 序列化,你应该把:

private String videoName;
private String producer;
private String rating;
private String genre;
private short videoNumber;
private float videoPrice;

在您的视频课程中。 您应该在Serializable类中放置的唯一静态变量是serialVersionUID(序列化和反序列化过程使用它)。如:

private static final long serialVersionUID = 1L;