Note that there are some explanatory texts on larger screens.

plurals
  1. POLoad/Store Objects in file in Java
    text
    copied!<p>I want to store an object from my class in file, and after that to be able to load the object from this file. But somewhere I am making a mistake(s) and cannot figure out where. May I receive some help?</p> <pre><code>public class GameManagerSystem implements GameManager, Serializable { private static final long serialVersionUID = -5966618586666474164L; HashMap&lt;Game, GameStatus&gt; games; HashMap&lt;Ticket, ArrayList&lt;Object&gt;&gt; baggage; HashSet&lt;Ticket&gt; bookedTickets; Place place; public GameManagerSystem(Place place) { super(); this.games = new HashMap&lt;Game, GameStatus&gt;(); this.baggage = new HashMap&lt;Ticket, ArrayList&lt;Object&gt;&gt;(); this.bookedTickets = new HashSet&lt;Ticket&gt;(); this.place = place; } public static GameManager createManagerSystem(Game at) { return new GameManagerSystem(at); } public boolean store(File f) { try { FileOutputStream fos = new FileOutputStream(f); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(games); oos.writeObject(bookedTickets); oos.writeObject(baggage); oos.close(); fos.close(); } catch (IOException ex) { return false; } return true; } public boolean load(File f) { try { FileInputStream fis = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(fis); this.games = (HashMap&lt;Game,GameStatus&gt;)ois.readObject(); this.bookedTickets = (HashSet&lt;Ticket&gt;)ois.readObject(); this.baggage = (HashMap&lt;Ticket,ArrayList&lt;Object&gt;&gt;)ois.readObject(); ois.close(); fis.close(); } catch (IOException e) { return false; } catch (ClassNotFoundException e) { return false; } return true; } . . . } public class JUnitDemo { GameManager manager; @Before public void setUp() { manager = GameManagerSystem.createManagerSystem(Place.ENG); } @Test public void testStore() { Game g = new Game(new Date(), Teams.LIONS, Teams.SHARKS); manager.registerGame(g); File file = new File("file.ser"); assertTrue(airport.store(file)); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload