-2

The assignment is to

Create a program that accepts and stores data from any Winter Olympics. The program should accept and store up to 6 competitor names for each event as well as the name of the event they participated in. It should also sort the scores or times of the events showing the medal winners only with the medal they won.

I have created an "Event" class which is going smoothly, but I run into problems when trying to write the "Event" objects to a file and read them.

Here is my method to create a new event:

public static void addEvent() {
    File events = new File("events.dat");
    Scanner input = new Scanner(System.in);
    String eventName;
    ArrayList<Event> olympics = new ArrayList<Event>();
    boolean fileExists = false;

    if (events.exists()) {
        fileExists = true;
    } else {
        try {
            events.createNewFile();
            System.out.println("File created");
        } catch (IOException e) {
            System.out.println("File could not be created");
            System.err.println("IOException: " + e.getMessage());
        }
    }

    System.out.print("Enter name of event: ");
    eventName = input.nextLine();
    olympics.add(new Event(eventName));

    //write Objects
    try {
        FileOutputStream out = new FileOutputStream(events, fileExists);
        ObjectOutputStream writeEvent = new ObjectOutputStream(out);
        writeEvent.writeObject(olympics);
        writeEvent.close();
    } catch (FileNotFoundException e) {
        System.out.println("File could not be found.");
        System.err.println("FileNotFoundException: " + e.getMessage());
    } catch (IOException e) {
        System.out.println("Problem with input/output.");
        System.err.println("IOException: " + e.getMessage());
    }
    menu();
}// end addEvent()

And my method to display the file:

public static void displayFile(File datFile) {
    File events = datFile;
    String output ="";

    //read objects
    try {
        FileInputStream in = new FileInputStream(events);
        ObjectInputStream readEvents = new ObjectInputStream(in);
        ArrayList<Event> recoveredEvents = new ArrayList<Event>();

        while (readEvents.readObject() != null) {
            recoveredEvents.add((Event)readEvents.readObject());
        }
        readEvents.close();
        for (Event e: recoveredEvents) {
            //System.out.println(e + "\n");
            output += e + "\n";
        }
    } catch(FileNotFoundException e) {
        System.out.println("File does not exist or could not be found");
        System.err.println("FileNotFoundException: " + e.getMessage());
    } catch (IOException e) {
        System.out.println("Problem reading file");
        System.err.println("IOException: " + e.getMessage());
    } catch (ClassNotFoundException e) {
        System.out.println("Class could not be used to cast object.");
        System.err.println("ClassNotFoundException: " + e.getMessage());
    }
    System.out.println(output);
    menu();
}// end displayFile()

This is a beginning class and handling files has been difficult to wrap my head around so far. Edit: Forgot to mention that it throws an IOException with the message "null" when I try to call displayFile().

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Aaron Cannon
  • 61
  • 1
  • 1
  • 8

1 Answers1

2
  1. You're getting EOFException. When readObject() gets to the end of the stream it doesn't return null. Check the Javadoc. It throws EOFException. You should catch this separately and not treat it as an error that needs reporting.

  2. You're throwing away every odd element. Your read loop should look like:

    while (true)
    {
        recoveredEvents.add((Event)readObject.readObject());
    }
    
  3. You can't append to a file written by ObjectOutputStream. You will get IOException: invalid type code AC when you get to the join when reading. There are ways around this but they are non-trivial. Keep the file open.

  4. new FileOutputStream() creates the file. All that exists/createNewFile crud before it is just a waste of time and space.
user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks for all the help. Now my only question is about your 3rd point. What do you mean by "keep the file open"? If I open the file with my addEvent() method, will the file still be open for me to use with my displayFile() method? – Aaron Cannon Jul 20 '15 at 15:49
  • 1
    Not calling `close` keeps the file open. – jfdoming Jul 20 '15 at 16:29