views:

221

answers:

4

Hi, I am having difficulties w/ writing and reading an array of objects from a file.

This is how my object looks like:

package registar;

import java.io.Serializable;

public class Vozilo implements Serializable {

    private static final long serialVersionUID = -5302010108271068350L;

    private String registracija;
    private String marka;
    private String kategorija;
    private int kubikaza;

    public Vozilo(String registracija, String marka, String kategorija,
            int kubikaza) {
        super();
        this.registracija = registracija;
        this.marka = marka;
        this.kategorija = kategorija;
        this.kubikaza = kubikaza;
    }
/* ALL GETTERS AND SETTERS ARE BELOW */

I am using basic GUI elements to get input and store it as object into a file...

I'm using the following code to write to a file named "test.dat" w/ dependable flag:

final ObjectOutputStream fos = new ObjectOutputStream(new FileOutputStream("test.dat", true));

Vozilo novo = new Vozilo(txtRegistracija.getText(), txtMarka.getText(), cbKat.getSelectedItem().toString(), Integer.parseInt(txtKubikaza.getText()) );

try {
    fos.writeObject(novo);
    fos.close();
    JOptionPane.showMessageDialog(unos, "Car was added!");
} catch (IOException e) {
    e.printStackTrace();
    JOptionPane.showMessageDialog(unos, "Car was NOT added!");
}

And the following code to read from file:

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.dat"));

ArrayList<Vozilo> list = new ArrayList<Vozilo>();
Vozilo vozilo = (Vozilo) ois.readObject();

list.add(vozilo);
ois.close();

for (Vozilo voz : list) {
    System.out.println("Marka: " + voz.getMarka() + "\n");
}

The problem is that I am unable to read all of the objects from file, only the first one is shown, and it iver returns IndexOutOfBounds exception :\ What am I doing wrong?

P.S. If the solution is obvious, don't bother, I haven't slept for more than 24hrs :P

Thank you in advance!!! Nikola

A: 

The code you show doesn't have a loop around the readObject call, so it would only read the first object. If that's not the problem, then please post the actual code that is not working.

Javid Jamae
A: 

You are only reading the first object you put in the file since this:

Vozilo vozilo = (Vozilo) ois.readObject();

is not in a loop.

What you could do is maybe to write the entire list as one object to the file and then read it back, instead of writing and reading one object at a time.

npinti
I'll try that asap :)
Nikola
Great idea! It worked! I did just like you suggested :D I have had few minor bugs, all fixed now. Solution accepted :) Thank you, npinti!
Nikola
You're Welcome :)
npinti
A: 

You can add a loop as follows:

Object obj = null;
while ((obj = ois.readObject()) != null) {
    if (obj instanceof Vozilo) {
    Vozilo vozilo = (Vozilo) obj;
    list.add(vozilo);
    }
}

However, you need to deal with EOFException when it reaches the end of the file.

PS: please use close() in finally block :)

Andy Lin
A: 

You are attempting the impossible. You can't append objects to a file with an ObjectOutputStream and get them back again, because ObjectOutputStreams have headers that won't be understood when you read the file back, unless you can organize to know where the boundaries between appends are and create a new ObjectInputStream each time as appropriate. In general this is infeasible un;ess you have a supplementary index for the file, in whcih case you might as well using blobs in a dataase. You could read the file in and write out a new one with all the original objects plus the new one each time, or better still keep the file open while you still have objects to write, if possible.

EJP
Thank you for the tips :)
Nikola