Skip to main content
gamedev.net
🔒 Locked

Database alternative

Started by MseMseM Jun 29, 2010 at 2:54 AM 1 replies 800+ views
Original Post
MseMseM
MseMseM
For Java, but maybe for another language too.
Actually not a library but really alternative to a database.

So just make all the objects you need to save serializable.
There must be an object that contains all the objects you need to save.
And than just in some few strings of code you can save all the data to a single file or load from one.

I use this trick in my browser game.
______________________________[my blog] [my browser game]
Kylotan
Kylotan
Are you asking a question? Or providing a library? Or...what?
MseMseM
MseMseM
it's clear
definitely not the first neither second
but i can easily to provide something
for Java:


package m.util;import java.io.*;public class Serialization implements Serializable {  private int a;  private String b;  /**   * on error throws RuntimeException   * if file doesn't exist creates it   */  public static void serialize(Serializable obj, String pathname) {    createNotExistingDirs(pathname);    ObjectOutputStream s = null;    try {      s = new ObjectOutputStream(new FileOutputStream(pathname));      s.writeObject(obj);      s.flush();      s.close();    } catch(Exception e) {      try {        s.close();      } catch(Exception ignored) {}      throw new RuntimeException(e);    }  }  /**   * on error throws RuntimeException   */  public static Object deserialize(String pathname) {    ObjectInputStream s = null;    try {      s = new ObjectInputStream(new FileInputStream(pathname));      Object o = s.readObject();      s.close();      return o;    } catch(Exception e) {      try {        s.close();      } catch(Exception ignored) {}      throw new RuntimeException(e);    }  }  private static void createNotExistingDirs(String path) {    try {      if (path.indexOf(File.separatorChar) == path.lastIndexOf(File.separatorChar)) return;      int pos = path.indexOf(File.separatorChar, 3);      while (pos != -1) {        new File(path.substring(0, pos)).mkdir();        pos = path.indexOf(File.separatorChar, pos+1);      }    } catch(Exception e) {      throw new RuntimeException(e);    }  }}


now saving or loading all the data of the project could be done with a single method

[Edited by - MseMseM on June 30, 2010 7:06:22 PM]
______________________________[my blog] [my browser game]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.