7

我在 Java 中有很多类,但这是我第一次尝试序列化任何东西。我制作了自己的类,其中包括一个数组列表。主要对象是这些类的数组列表。我相信我所做的一切都是正确的,但是当我重新读取它时,arraylist 总是空的。

主要(主要是测试)类:

import java.io.*;
import java.util.ArrayList;

public class IOTest {
public static void main(String[] args) {
    ArrayList<Info> master = new ArrayList <Info>();
    Info a = new Info("a");
    Info b = new Info("a");
    Info c = new Info("a");
    master.add(a);
    master.add(b);
    master.add(c);
    print(master);
    save(master);
    ArrayList<Info> loaded = new ArrayList <Info>();
    load(loaded);
    System.out.println("Loaded List:");
    System.out.println("Loaded Size:" + loaded.size());
    print(loaded);
}

public static void save(ArrayList a){
    File f = new File("savefile.dat");
    try {
        FileOutputStream fos = new FileOutputStream(f);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(a);
        fos.flush();
        fos.close();
     } catch (IOException ioe) {
         System.out.println("Failed to save");
     };
}

public static void load(ArrayList a){
    File f = new File("savefile.dat");
    try {
        FileInputStream fis = new FileInputStream(f);
        ObjectInputStream ois = new ObjectInputStream(fis);
        try {
          a = (ArrayList<Info>) ois.readObject();
        } catch (ClassNotFoundException cnfe) { 
            System.out.println("Failed to load");
        }
        fis.close();
     } catch (IOException ioe) {
         System.out.println("Failed to load");
     }
}

public static void print(ArrayList a){
    System.out.println(a);
}
}

自定义数据结构:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;


public class Info implements Serializable {
private static final long serialVersionUID = -5781559849007353596L;

ArrayList<String> list;
public Info( String e) {
    list = new ArrayList<String>();
    list.add(e);
}
@Override
public String toString() {
    return list.toString();
}

private void readObject(ObjectInputStream aInputStream) throws ClassNotFoundException, IOException {
    aInputStream.defaultReadObject();
}

private void writeObject(ObjectOutputStream aOutputStream) throws IOException {
    aOutputStream.defaultWriteObject();
}
}

如果有人能指出我做错了什么,我将不胜感激。

4

3 回答 3

5

这个问题与序列化无关。

您正在更改方法reference of passed parameter内的列表,load()这将不起作用,因为更改的引用在该方法之前只有范围。将负载更改为以下

public static ArrayList<Info> load() {//Change return type
    File f = new File("savefile.dat");
    try {
        FileInputStream fis = new FileInputStream(f);
        ObjectInputStream ois = new ObjectInputStream(fis);
        try {
            ArrayList<Info> a = (ArrayList<Info>) ois.readObject();//get the object that is read
            return a;
        } catch (ClassNotFoundException cnfe) {
            System.out.println("Failed to load");
        }
        fis.close();
    } catch (IOException ioe) {
        System.out.println("Failed to load");
    }
    return null;
  }

并使用返回的对象。

 ArrayList<Info> loaded = load();
于 2012-10-09T08:02:29.187 回答
0

您的代码没有任何问题。

这里有一个逻辑问题:

ArrayList<Info> loaded = new ArrayList <Info>();
load(loaded);

您正在尝试加载一个空的 ArrayList (这就是它什么都不打印的原因)。我猜你要加载的是主文件

load(master);
于 2012-10-09T08:02:57.663 回答
0

试试这个代码......

结果 :

[[a], [a], [a]] 加载列表:加载大小:3 [[a], [a], [a]]

package com.palit.java.serialize;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Init {
public static void main(String[] args) {
    ArrayList<Info> master = new ArrayList<Info>();
    Info a = new Info("a");
    Info b = new Info("a");
    Info c = new Info("a");
    master.add(a);
    master.add(b);
    master.add(c);
    print(master);
    save(master);
    List<Info> loaded = new ArrayList<Info>();
    loaded = load();
    System.out.println("Loaded List:");
    System.out.println("Loaded Size:" + loaded.size());
    print(loaded);
}

public static void save(ArrayList a) {
    File f = new File("savefile.dat");
    try {
        FileOutputStream fos = new FileOutputStream(f);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(a);
        fos.flush();
        fos.close();
    } catch (IOException ioe) {
        System.out.println("Failed to save");
    }
    ;
}

public static List load() {
    List list = new ArrayList();
    File f = new File("savefile.dat");
    try {
        FileInputStream fis = new FileInputStream(f);
        ObjectInputStream ois = new ObjectInputStream(fis);
        try {
            list = (ArrayList<Info>) ois.readObject();
        } catch (ClassNotFoundException cnfe) {
            System.out.println("Failed to load");
        }
        fis.close();
    } catch (IOException ioe) {
        System.out.println("Failed to load");
    }

    return list;
}

public static void print(List a) {
    System.out.println(a);
}
}
于 2012-10-09T08:08:09.970 回答