Storing arrays (hashmaps)

Discussion in 'Plugin Development' started by Infernus, Mar 13, 2011.

Thread Status:
Not open for further replies.
  1. Offline


    Is it possible to store arrays (hashmaps) so you can later reload them? If yes, could you attach a snippet, please?

    Thanks! :)
  2. Offline

    Daniel Heppner

    I would like to know too, I think you'd use some kind of database. Perhaps putting them into a .yml file. Anyways, I like your signature. :p
  3. Offline


    Haha, thanks, I think it might be quite hard for people to get the second one, though. Are you also in this kind of music? :)

    And yes, I think it should be that way, yml files..
  4. Offline


    Something like this should work:

    BufferedWriter writer = null;
        writer = new BufferedWriter(new FileWriter("file.txt"));
        for (Object key: hashMap.keySet())
            writer.write(key + ":" + hashMap.get(key));
    catch (Exception e) {}
    It'll write each entry in the HashMap to a line in file.txt. You can then read the file and load them back into a HashMap.
  5. Offline

    Edward Hand

    That'll be fine if your hash-map is full of numbers and strings but if it's full of players and itemStacks or something you might have some trouble reading them back in again...
  6. Offline


    Alright, and how about achieving them back into your hashmap? :)
  7. Offline


    Depends what you're trying to get. This is just an example of one type of hashmap :

            Scanner in = new Scanner(new File("file.txt"));
            HashMap<Integer, Double> map = new HashMap<Integer, Double>();
                String line = in.nextLine();
                if(line != null){
                    try {
                        map.put(Integer.parseInt(line.split(":")[0]), Double.parseDouble(line.split(":")[1]));
                    } catch(Exception e){
                        System.out.println("Error reading save file.");
  8. Offline


    Ah, that's actually very simple! Thanks for sharing this! And about Player hashmaps, you can also store just the playername if that's possible in the relevant plugin.
  9. Offline


    HashMaps are serializable. Use ObjectOutputStream to write them to file, and then ObjectInputStream to read them (don't forget to cast from Object to HashMap).
  10. Offline


    Saving a Object like a Player to a File would make that file huge no?
Thread Status:
Not open for further replies.

Share This Page