How to save and read a list of object arrays in java?

I'm having difficulty writing and reading an array of objects from a file.

This is what 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 use the basic GUI elements to enter input and save it as an object to a file ...

I use the following code to write to a file called test.dat with a reliable 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 a 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 can not read all the objects from the file, only the first one is shown, and it returns an IndexOutOfBounds exception: \ What am I doing wrong?

PS If the solution is obvious, do not worry, I have not slept for more than 24 hours: P

Thanks in advance! Nikola

+4
source share
5 answers

You only read the first object that you put in the file:

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

not in a loop.

What you can do is perhaps write the entire list as one object in a file, and then read it, instead of writing and reading one object at a time.

+2
source

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 an EOFException when it reaches the end of the file.

PS: use finally () in the finally block :)

+3
source

The code you show does not have a loop around the readObject call, so it will only read the first object. If this is not a problem, then please write the actual code that does not work.

+1
source

You are trying to do the impossible. You cannot add objects to a file using ObjectOutputStream and return them again, because ObjectOutputStreams has headers that will not be understood when reading a file, if you cannot arrange to find out where the boundaries between the additions are and create a new ObjectInputStream every time it's necessary. In general, this is not possible because you have an additional index for the file, in which you could also use blobs in dataase. You can read the file and write a new one with all the original objects plus a new one each time, or, nevertheless, keep the file open while there are still objects for writing, if possible.

0
source

You read only the first object. So you need a loop in which you can continue reading from the reader (using the readObject method) until it reaches the end.

 Object obj = null; while ((obj = inputReader.readObject()) != null) { if (obj instanceof Indexing) { Indexing newObject = (Indexing) obj; list.add(newObject); } } 
0
source

All Articles