从文件中知道可序列化对象的数量

Know the amount of objects Serializable from file

我正在使用 Serializable 从文件中读取对象:

    public ArrayList<Object> deserialzePerson(String filename) {
    Object obj = null;
    ObjectInputStream ois;
    try {
        ois = new ObjectInputStream(new FileInputStream(filename));
        for (int i = 0; i < 100; i++) {
            obj = (Object) ois.readObject();
            ObjectArray.add(obj);
        }
    } catch (Exception e) {
    }
    return ObjectArray;
}

但是,我不知道文件中的对象数量,因此在 for 循环中使用了数字“100”。如果少于 100,异常将启动并且一切都按预期进行。尽管如此,我发现这个解决方案很差,因为它依赖于捕获错误。有没有办法为文件中的对象数量设置 for 循环的限制?

例如,当我使用 .hasNext(); 读取 .txt 文件时,是否有类似这样的对象?

简短版本 - 你不能。长版本,如果您将对象数量作为 int (或 long )写入流中的第一个位置 - 然后先读取它,则可以。

您还可以依赖 EOFException,如果不再有对象,它将被抛出

public void serializePerson(String filename, List<Person> persons) {
    try (FileOutputStream fos = new FileOutputStream(filename);
         ObjectOutputStream ous = new ObjectOutputStream(fos)) {

        ous.writeInt(persons.size());
        for (Person person : persons) {
            ous.writeObject(person);
        }
    } catch (Exception e) {
    }
}

public List<Person> deserializePerson(String filename) {
    List<Person> result = new ArrayList<>();
    try (FileInputStream fis = new FileInputStream(filename);
         ObjectInputStream ois = new ObjectInputStream(fis)) {
        int size = ois.readInt();
        for (int i = 0; i < size; i++) {
            Person person = (Person) ois.readObject();
            result.add(person);
        }
    } catch (Exception e) {
    }
    return result;
}

但是,我不知道文件中的对象数量,所以在for-loop中使用了数字“100” 读取对象的 for-loop 的最佳替代方法是

try {
    int currentCounter = 0; 
    ois = new ObjectInputStream(new FileInputStream(filename));
    for (Object obj = null; (obj = ois.readObject()) != null ; currentCounter++) 
    {
        ObjectArray.add(obj);
        // currentCounter is the way out in this case, but I can give more explanations
    }
} catch (Exception e) 
 {
    if( e instanceof EOFException )
    {
        System.err.println( e.getClass() + "=" + e.getMessage() );
    }
    else
    {
        System.err.println( "UNKNOWN INSTANCE: " + e.getClass() + "=" + e.getMessage() );
    }
 }

围绕着一堆 event-listener 机制,你甚至可以做得更好。问题是在计划读取

文件之前,您是否需要预先或post 了解对象数量

您可以写大小,或者更简单的解决方案是写 List is which also an object。

public static void save(String filename, Object o) {
    try (FileOutputStream fos = new FileOutputStream(filename);
         ObjectOutputStream ous = new ObjectOutputStream(fos)) {
        ous.writeObject(o);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

public static <T> T load(String filename) {
    try (FileInputStream fis = new FileInputStream(filename);
         ObjectInputStream ois = new ObjectInputStream(fis)) {
        return (T) ois.readObject();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

例如

List<Person> people = load("people.dat");
people.add(new Person());
save("people.dat", people);

这使您可以用更少的代码编写对象或任何类型。