How to save a hashmap?

Discussion in 'Plugin Development' started by Codex Arcanum, Dec 31, 2011.

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

    Codex Arcanum

    Pretty new at programing in Java, so please bear with me.

    I have a HashMap<String, Location>, and I need to save it to a file or a database so it persists through server restarts. So far, I know that I could save it to a MySQL database (no clue how to do this), create a custom class that would make a serializable location and serialize it (no clue here either), use .toString() and then find a way to recover it to a hashmap, or break it down into its component parts and save those to a text file. Which of these would be easiest/best?
  2. Offline


  3. Offline


    You can do what I did when I started programming :

    1. create a file
    1. File file = new File(this.getDataFolder, "filename.dat");
    2. if(!file.exsits()){
    3. try{
    4. file.createNewFile();
    5. }catch(IOException ex){
    6. System.out.println("file creation failed!");
    7. }
    8. }

    2. convert location to string method
    1. public String locToString(Location loc){
    2. String wname = loc.getWorld().getName();
    3. String x = loc.getX().toString();
    4. String y = loc.getY().toString();
    5. String z = loc.getZ().toString();
    6. String str = String.format("%s, %s, %s, %s",wname ,x ,y, z);
    7. return str;
    8. }

    3. create a writing method
    1. public void write(File file, HashMap<String, Location> data){
    2. //using a formatter to write to a file
    3. Formatter formatter;
    4. try{
    5. formatter = new Formatter(file);
    6. System.out.println("Failed to find the desired file");
    7. }
    8. //creating a for loop
    9. for(String s : data.ketSet()){
    10. formatter.format("%s: %s", s, locToString(data.get(s)));
    11. }
    12. formatter.close();
    13. }

    Done, all saved.

    4. converting string data to Location data:
    1. public Location stringToLoc(String s){
    2. String[] data = s.split(", ");
    3. World w = getServer().getWorld(data(0));
    4. double x = Double.parseDouble(data(1));
    5. double y = Double.parseDouble(data(2));
    6. double z = Double.parseDouble(data(3));
    7. return new Location(w, x, y, z);
    8. }

    5. reading files using scanner
    1. public HashMap<String, Location> read(File file){
    2. HashMap<String, Location> data = new HashMap<String, Location>();
    3. Scanner scanner;
    4. try{
    5. scanner = new Scanner(file);
    6. System.out.println("Failed to find the desired file");
    7. }
    8. while(scanner.hasNextLine()){
    9. String[] line = scanner.nextLine().split(": ");
    10. data.put(line(0), stringToLoc(line(1)));
    11. }
    12. scanner.close();
    13. return data;
    14. }

    You can also use bukkit configuration instead of scanner/ formatter but it's up to you.
    Note that you might get some errors because it's possible that I mispelled some methods (it's hard to write every thing correctly using a minigalaxy cellphone xD). make sure to correct any spelling error (once again, sorry if there are any) and enjoy

    edit : the method in the comment above this one is probably more simple :)
Thread Status:
Not open for further replies.

Share This Page