Search code examples
javainheritancearraylistoutputobjectoutputstream

ObjectOutputStream writing array to file with inherited classes not writing to file


My assignment is to save a list of employees as a binary file (and later read from it). I'm working on the output portion now, below is the block of the function that is in question. This function does have about 10 more lines but they edit things on the TextField arraylist.

The Employee class is the parent of both the Supervisor and Secretary classes. all is the ArrayList that holds all the employee, secretary, and employee objects.

I'm using netbeans 8.0.2, when the program runs and I click the save button in the gui (onActionEvent() is this function) there are no compiler errors. The "IO Error" or "No Permissions..." doesn't output. Ive tried saving both with and without the employees.dat file being already created.

I'm not really sure what to do at this point, I contemplated saving each object as a the collection of int, String, etc but that's dumb, it should be able to work this way... right?

EDIT: Employee, Supervisor, and Secretary are all Serializable.

private void saveChanges(ArrayList<Employee> all, ArrayList<TextField> text, int index) {


    try ( ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("employees.dat", true)); ) {

        for (int i = 0; i < all.size(); i++) {
            if (all.get(i).getClass() == (new Secretary().getClass()))
                output.writeObject(new Secretary((Secretary) all.get(i)));
            else if (all.get(i).getClass() == (new Supervisor().getClass()))
                output.writeObject(new Supervisor((Supervisor) all.get(i)));
            else
                output.writeObject(new Employee(all.get(i)));
        }

        output.flush();
    } catch (IOException io) {
        io.printStackTrace();
    } catch (SecurityException se) {
        se.printStackTrace();
    }
}

EDIT:

I have edited the try-catch to this code...

try ( ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("employees.dat", true)); ) {

                output.writeObject(all);


        output.flush();
        output.close();
    } catch (IOException io) {
        io.printStackTrace();
    } catch (SecurityException se) {
        se.printStackTrace();
    }

Still not writing to file. I have permissions in the folder the .java files are in.


Solution

  •     try ( ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("employees.dat", true)); ) {
    

    Change true to false, or remove it. You can't append to files created as object output streams, at least not like this, and it's debatable whether you're even supposed to be doing so.

            for (int i = 0; i < all.size(); i++) {
                if (all.get(i).getClass() == (new Secretary().getClass()))
                    output.writeObject(new Secretary((Secretary) all.get(i)));
                else if (all.get(i).getClass() == (new Supervisor().getClass()))
                    output.writeObject(new Supervisor((Supervisor) all.get(i)));
                else
                    output.writeObject(new Employee(all.get(i)));
            }
    

    Change this entire mess to this:

    output.writeObject(all);
    

    If this still doesn't work there must have been an exception somewhere, and you're just going to have to find it, print it, and post it. Edit it into your question. Or else you're looking at the wrong file.

    NB you will also have to change the code that reads the file, to just read the list in a single readObject() call.