Saving nested hashmaps to a file

Discussion in 'Plugin Development' started by MeneXia, Dec 28, 2011.

Thread Status:
Not open for further replies.
  1. Saving this:
    PHP:
    public Map<StringMap<ShortInteger>> scrollHM = new HashMap<StringMap<ShortInteger>>();
    into a file.

    I know of the SLAPI (Saving and Loading API) from the wiki, but it seems like there's something missing in my code.

    This is the class where it saves and loads the HashMap, the class is called GSDataHandler:

    PHP:
    public static void save(Object objString paththrows Exception {
            
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
            
    oos.writeObject(obj);
            
    oos.flush();
            
    oos.close();
        }

        public static 
    Object load(String paththrows Exception {
            
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
            
    Object result ois.readObject();
            
    ois.close();
            return 
    result;
            }
    I mention the code neatly in the onEnable and onDisable methods of my main class:
    onEnable:
    PHP:
    File scrollData = new File(getDataFolder(), "scrolls.dat");

    public 
    void onEnable() {
    if (
    this.scrollData.exists()) {
            try {
                
    scrollHM = (Map<StringMap<ShortInteger>>)GSDataHandler.load(
                        
    "plugins" File.separator "GuardianScrolls" File.separator "scrolls.dat");
            } catch (
    Exception e) {
                
    e.printStackTrace();
            }
        }
    }
    onDisable:
    PHP:
    File scrollData = new File(getDataFolder(), "scrolls.dat");

    public 
    void onDisable() {
        
    this.ScrollNames.clear();
            try {
                
    GSDataHandler.save(scrollHM"plugins" File.separator "GuardianScrolls" File.separator "scrolls.dat");
            } catch (
    Exception e) {
                
    e.printStackTrace();
            }
    }
    The Result: A file in the data folder of my plugins gets created, and increases in file size whenever a key/value pair is put in. However, when restarting the server, it does not retain the data from the file, although "scrolls.dat" is still present. There are no errors in the console when enabling and disabling the plugin.

    If you need extra clarification, please tell me.
     
  2. Offline

    MrMag518

    Sorry for late response, but I may see your problem;
    this.ScrollNames.clear();
    That clears the HashMap for all data.
     
  3. scrollHM = (Map<String, Map<Short, Integer>>)GSDataHandler.load(
    "plugins" + File.separator + "GuardianScrolls" + File.separator + "scrolls.dat");

    GSDataHandler.save(scrollHM, "plugins" + File.separator + "GuardianScrolls" + File.separator + "scrolls.dat");

    why you defining the location of the file again if you defined
    Code:java
    1. File scrollData = new File(getDataFolder(), "scrolls.dat");[/syntac] bofere it, just use scrollData.toAbsolutePath()
     
Thread Status:
Not open for further replies.

Share This Page