Know the number of objects that can be serialized from a file

I am reading objects from a file using 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;
}

However, I do not know the number of objects in the file and use the number "100" in the for-loop. If there are less than 100, the exception will begin, and everything will be as expected. However, I think this is a bad decision because it depends on fishing errors. Is there a way to set a limit on the loop for the number of objects in a file?

For example, when reading from a .txt file that I used .hasNext();, is there something similar for objects?

+4
source share
4 answers
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;
}
+4
source

"100" for-loop 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-listenener, . , , .

+1

- List, .

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);

.

+1

- . , , int ( long) - .

EOFException, ,

0

Source: https://habr.com/ru/post/1668868/


All Articles