سؤال

Today I started using serialized object in java, I'm new at it and I have some problems when I try to deserialize. I have this file where I write all my Account objects, it writes fine I guess. The problem is I don't know how to refer to a specific object from that file, or how could I get all of them into a list? and then refer to it. This is how i'm trying to read them:

public void readAccount(Account e) {
/*  List<Account> results = new ArrayList<Account>();
    try {
        FileInputStream fileIn = new FileInputStream("test.txt");
        ObjectInputStream in = new ObjectInputStream(fileIn);
        for (int i = 0; i < accBank.size(); i++) {
            results.add((Account) 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;
    }
    for (Account acc : results) {
        System.out.println(toString(acc));
        if(e.getAcc_no() == acc.getAcc_no())
        {System.out.println("Deserialized Account...");
        System.out.println(toString(e));
        }
    }
    */
        List<Account> results = new ArrayList<Account>();
        Account acc = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("test.txt");
            while (true) {
                ObjectInputStream ois = new ObjectInputStream(fis);
                results.add((Account) ois.readObject());
                acc = (Account) ois.readObject();
            }
        } catch (Exception ignored) {
            // as expected
        } finally {
            if (fis != null)
                try {
                    fis.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
        }
        System.out.println("results = " + results);

        for (Account ac : results) {
            System.out.println(toString(ac));
            if(e.getAcc_no() == ac.getAcc_no())
            {System.out.println("Deserialized Account...");
            System.out.println(toString(e));
            }
    }
}

And this is how I write them:

public void writeAccount(Account e) {
    try {
        ObjectOutputStream os1 = new ObjectOutputStream(
                new FileOutputStream("test.txt", true));
        os1.writeObject(e);
        os1.close();
    } catch (Exception exc) {
        exc.printStackTrace();
    }
}

Edit:

public void writeFile() {
    for (int i = 0; i < accBank.size(); i++) {
        writeAccount(retAcc(i));
    }
}

Can any of you tell me what im doing wrong? I also tried other examples from other questions and didn't work.

هل كانت مفيدة؟

المحلول

What you're doing wrong is that you use several ObjectOutputStreams to write to the same file (which is not a txt file, BTW, since it contains binary data), and use a single ObjectInputStream to read all the accounts. As a consequence, a new serialization header is written each time you write an account, and the ObjectInputStream doesn't expect that.

The best way to write a list of accounts is to do just that: you store the accounts into a List<Account>, and write the list. To read the list of accounts, you do just that: you read a single object from the file, and cast it to List<Account>.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top