java - Reading unknown number of objects from file -
the assignment
create program accepts , stores data winter olympics. program should accept , store 6 competitor names each event name of event participated in. should sort scores or times of events showing medal winners medal won.
i have created "event" class going smoothly, run problems when trying write "event" objects file , read them.
here method create 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 not 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 not found."); system.err.println("filenotfoundexception: " + e.getmessage()); } catch (ioexception e) { system.out.println("problem input/output."); system.err.println("ioexception: " + e.getmessage()); } menu(); }// end addevent()
and method display 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(); (event e: recoveredevents) { //system.out.println(e + "\n"); output += e + "\n"; } } catch(filenotfoundexception e) { system.out.println("file not exist or not 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 not used cast object."); system.err.println("classnotfoundexception: " + e.getmessage()); } system.out.println(output); menu(); }// end displayfile()
this beginning class , handling files has been difficult wrap head around far. edit: forgot mention throws ioexception message "null" when try call displayfile().
you're getting
eofexception
. whenreadobject()
gets end of stream doesn't return null. check javadoc. throwseofexception.
should catch separately , not treat error needs reporting.you're throwing away every odd element. read loop should like:
while (true) { recoveredevents.add((event)readobject.readobject()); }
you can't append file written
objectoutputstream
.ioexception: invalid type code ac
when join when reading. there ways around non-trivial. keep file open.new fileoutputstream()
creates file. exists/createnewfile crud before waste of time , space.
Comments
Post a Comment