title: Java Serializable Saving
body: <p>Well I have this class for a game I'm making, and I'm trying to save the ArrayList notes. When I logout, it properly print the size, for example if I have 5 notes, when I log out notes.getSize() would be 5, but when I log in, it gets reset back to nothing. Why isn't notes saving?</p>
  
  <pre><code>public class Notes implements Serializable {
  
      private static final long serialVersionUID = -4947870743226160329L;
      private ArrayList&lt;Note&gt; notes = new ArrayList&lt;Note&gt;(30);
  
      public class Note implements Serializable {
  
          private static final long serialVersionUID = -4589885080580317958L;
  
          private int color = 0;
          private String text = "";
  
          public Note(int color, String text) {
              this.setColor(color);
              this.setText(text);
          }
  
          public void setText(String text) {
              this.text = text;
          }
  
          public String getText() {
              return text;
          }
  
          public void setColor(int color) {
              this.color = color;
          }
  
          public int getColor() {
              return color;
          }
      }
  
      private transient Player player;
  
      public Notes(Player p) {
          this.player = p;
      }
  
      public void addNote(String text) {
          System.out.println("Note Text: "+text);
          if (text.length() &gt; 50) {
              player.getPackets().sendGameMessage("You can only enter notes up to 50 characters!");
              return;
          }
          if (notes.size() &lt; 30) {
              notes.add(new Note(0, text));
          } else {
              player.getPackets().sendGameMessage("You cannot add more then 30 notes!");
              return;
          }
          int NoteId = notes.size() - 1;
          player.getPackets().sendConfig(1439, NoteId);
          player.getTemporaryAttributtes().put("selectedNote", NoteId);
          refreshNotes(false);
      }
  
      public void addNote(String text, int color) {
          notes.add(new Note(color, text));
      }
  
      public void loadNotes() {
          player.getPackets().sendIComponentSettings(34, 9, 0, 30, 2621470);
          player.getPackets().sendHideIComponent(34, 3, false);
          player.getPackets().sendHideIComponent(34, 44, false);
          player.getPackets().sendIComponentText(34, 13, "Loading notes&lt;br&gt;Please wait...");
          player.getPackets().sendConfig(1439, -1);
          refreshNotes(true);
      }
  
      public void refreshNotes(boolean sendStartConfigs) {
          for (int i = 0; i &lt; 30; i++) {
              player.getPackets().sendGlobalString(149 + i, i &lt; notes.size() ? notes.get(i).getText() : "");
          }
          if (sendStartConfigs) {
              for (int i = 1430; i &lt; 1450; i++)
                  player.getPackets().sendConfig(i, i);
          }
          player.getPackets().sendConfig(1440, getFirstTotalColorValue());
          player.getPackets().sendConfig(1441, getSecondTotalColorValue());
      }
  
  
      public int intColorValue(int color, int noteId) {
          return (int) (Math.pow(4, noteId) * color);
      }
  
      public int getFirstTotalColorValue() {
          int Color = 0;
          for (int i = 0; i &lt; 15; i++) {
              if (notes.size() &gt; i)
                  Color += intColorValue(notes.get(i).getColor(), i);
          }
          return Color;
      }
  
      public int getSecondTotalColorValue() {
          int color = 0;
          for (int i = 0; i &lt; 15; i++) {
              if (notes.size() &gt; (i + 16))
                  color += intColorValue(notes.get(i + 16).getColor(), i);
          }
          return color;
      }
  
      public void deleteSelectedNote() {
          if ((int)player.getTemporaryAttributtes().get("selectedNote") &gt; -1) {
              int slot = (int) player.getTemporaryAttributtes().get("selectedNote");
              notes.remove(slot);
              player.getTemporaryAttributtes().put("selectedNote", -1);
              player.getPackets().sendConfig(1439, -1);
              refreshNotes(false);
          }
      }
  
      public void clear() {
          notes.clear();
          refreshNotes(false);
      }
  
      public void editNote(String string, int index) {
          notes.get(index).setText(string);
          refreshNotes(false);
      }
  
      public void setColor(int color, int index) {
          notes.get(index).setColor(color);
          refreshNotes(false);
      }
  
      public void deleteNote(int slot) {
          notes.remove(slot);
          refreshNotes(false);
      }
  
      public void setNotes(ArrayList&lt;Note&gt; setNotes) {
          notes = setNotes;
          refreshNotes(false);
      }
  
  }
  </code></pre>
  
  <p>And here is the class I manage saving/loading in</p>
  
  <p>public class SerializableFilesManager {</p>
  
  <pre><code>private static final String PATH = "data/characters/";
  private static final String BACKUP_PATH = "data/charactersBackup/";
  
  public synchronized static final boolean containsPlayer(String username) {
      return new File(PATH + username + ".p").exists();
  }
  
  public synchronized static Player loadPlayer(String username) {
      try {
          return (Player) loadSerializedFile(new File(PATH + username + ".p"));
      } catch (Throwable e) {
          Logger.handle(e);
      }
      try {
          Logger.log("SerializableFilesManager", "Recovering account: "
                  + username);
          return (Player) loadSerializedFile(new File(BACKUP_PATH + username
                  + ".p"));
      } catch (Throwable e) {
          Logger.handle(e);
      }
      return null;
  }
  
  public static boolean createBackup(String username) {
      try {
          Utils.copyFile(new File(PATH + username + ".p"), new File(
                  BACKUP_PATH + username + ".p"));
          return true;
      } catch (Throwable e) {
          Logger.handle(e);
          return false;
      }
  }
  
  public synchronized static void savePlayer(Player player) {
      try {
          storeSerializableClass(player, new File(PATH + player.getUsername()
                  + ".p"));
      } catch (ConcurrentModificationException e) {
          //happens because saving and logging out same time
      } catch (Throwable e) {
          Logger.handle(e);
      }
  }
  
  public static final Object loadSerializedFile(File f) throws IOException,
          ClassNotFoundException {
      if (!f.exists())
          return null;
      ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
      Object object = in.readObject();
      in.close();
      return object;
  }
  
  public static final void storeSerializableClass(Serializable o, File f)
          throws IOException {
  
      ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
      out.writeObject(o);
      out.close();
  }
  
  private SerializableFilesManager() {
  
  }
  </code></pre>
  
  <p>}</p>