Solved Loading a Hashmap from .bin file

Discussion in 'Plugin Development' started by Asien, Jul 10, 2014.

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

    Asien

    I am new to this whole Java thing, so I don't have any experience with handling files. I made a plugin that saved a Hashmap in a .bin file to load on start up. I can save just fine but loading seems to never work. All I get from this code is a null value when there is clearly data in there.

    Code for the load() method:
    Code:java
    1. public static HashMap<String, ArrayList<String>> load(String path) {
    2. Object result = null;
    3. BufferedReader br = null;
    4. try {
    5. br = new BufferedReader(new FileReader(path));
    6. } catch (FileNotFoundException e1) {
    7. e1.printStackTrace();
    8. }
    9.  
    10. try {
    11. if (br.readLine() == null) {
    12. result = new HashMap<String, ArrayList<String>>();
    13. } else {
    14. try {
    15.  
    16. new FileInputStream(path));
    17. result = ois.readObject();
    18. ois.close();
    19.  
    20. } catch (Exception e) {
    21. e.printStackTrace();
    22. }
    23.  
    24. }
    25. } catch (IOException e) {
    26. e.printStackTrace();
    27. }
    28. try {
    29. br.close();
    30. } catch (IOException e) {
    31. e.printStackTrace();
    32. }
    33. return (HashMap<String, ArrayList<String>>) result;
    34.  
    35. }
    36.  
     
  2. Offline

    Asien

    bump
     
  3. Offline

    teej107

    Add in debug code.
     
  4. Offline

    fireblast709

    Asien why not use YAML?
     
  5. Offline

    Asien

    I looked into how to use the debugger in Eclipse for Bukkit plugins and somewhere along the way the issue resolved itself without changing the code I gave in the OP. It seems the code I gave for the load() method works just fine. So, thanks, somehow that helped and now everything is working as it should.

    I could use YAML, but I wanted to try something different for the sake of learning.
     
  6. Offline

    ResultStatic

    Asien i would suggest using yaml, but here is the code i use to read an entire file which i dont think u can do with regular yaml. this returns a list where each string is a line

    Code:
     public static List<String> readLines(String file, String folder){
              List<String> lines = new ArrayList<String>();
              try {
              BufferedReader reader = new BufferedReader(new FileReader(file));
              String line;
                while((line = reader.readLine()) != null){
                  lines.add(line); 
                  }
                  reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return lines;
          }
       
          public static List<String> readLinesWith(String file, String folder, String prefix){
              List<String> lines = new ArrayList<String>();
              try {
              BufferedReader reader = new BufferedReader(new FileReader(file));
              String line;
                while((line = reader.readLine()) != null){
                if (line.startsWith(prefix)){
                  lines.add(line); 
                  }
                }
                  reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return lines;
          }
    then you can get a format like this. Kills: 100 with

    Code:
    if (line.contains(":")){
        header = (line.split(":") [0]);
    which will equal Kills.

    and after that i use yaml to get the object after it which would be 100.
     
  7. Offline

    Asien

    ResultStatic Thanks. I just used some of that for another part of the plugin! Very helpful.
     
Thread Status:
Not open for further replies.

Share This Page