save method just save the last thing of an hashmap..

Discussion in 'Plugin Development' started by Uniclaw, Jan 7, 2013.

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

    Uniclaw

    Hi!

    I've done a saving method, but just the last thing wich i putted into the hashmap is saved -.-
    Code:
    public void save(){
    if(! new File(getDataFolder(), "bd.dat").exists()){
    try {
    new File(getDataFolder(), "bd.dat").createNewFile();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    for(Location l : data.keySet()){
    Data bd = data.get(l);
    String ut = String.valueOf(bd.useTime());
    String t  = String.valueOf(bd.getTime());
    String up = String.valueOf(bd.getPermissionUsage());
    String p  = String.valueOf(bd.getPermission());
    String a  = String.valueOf(bd.isActive());
     
    try {
    BufferedWriter out = new BufferedWriter(new FileWriter(new File(getDataFolder(), "bd.dat")));
    out.write(
    new StringBuilder()
    .append(l.getWorld().getName()).append(",").append(l.getX()).append(",").append(l.getY()).append(",").append(l.getZ())
    .append(";")
    .append(ut).append(",").append(t).append(",").append(up).append(",").append(p).append(",").append(a).toString());
    out.newLine();
    out.close();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }
    Why :eek:?
     
  2. Offline

    fireblast709

    you keep overwriting your own file. Create the BufferedWriter outside of the for-loop
     
  3. Offline

    Uniclaw

    Doesn't works too :( There is just one thing saved :/
     
  4. Offline

    EnvisionRed

    Change the filewriter constructor to
    Code:
    new FileWriter(new File(getDataFolder(), "bd.dat"), true)
    I'm working from memory but I'm fairly sure that FileWriter can also take a boolean argument which is whether to append to the file, rather than overwriting. If you don't include it, it defaults to overwriting.
     
  5. Offline

    Uniclaw

    Doesn't woks too :'(!
     
  6. Offline

    EnvisionRed

  7. Offline

    fireblast709

    Code:java
    1. public void save()
    2. {
    3. File data = new File(getDataFolder(), "bd.dat");
    4. if(!data.exists())
    5. {
    6. try
    7. {
    8. data.mkdirs();
    9. new data.createNewFile();
    10. }
    11. catch (IOException e)
    12. {
    13. e.printStackTrace();
    14. return;
    15. }
    16. }
    17.  
    18. BufferedWriter out = null;
    19. try
    20. {
    21. out = new BufferedWriter(new FileWriter(data));
    22.  
    23. for(Location l : data.keySet())
    24. {
    25. Data bd = data.get(l);
    26. String ut = String.valueOf(bd.useTime());
    27. String t = String.valueOf(bd.getTime());
    28. String up = String.valueOf(bd.getPermissionUsage());
    29. String p = String.valueOf(bd.getPermission());
    30. String a = String.valueOf(bd.isActive());
    31.  
    32. out.write(
    33. new StringBuilder()
    34. .append(l.getWorld().getName()).append(",").append(l.getX()).append(",").append(l.getY()).append(",").append(l.getZ())
    35. .append(";")
    36. .append(ut).append(",").append(t).append(",").append(up).append(",").append(p).append(",").append(a).toString()
    37. );
    38. out.newLine();
    39. }
    40. }
    41. catch(IOException ex)
    42. {
    43. //log.info("Failed to write data to file");
    44. ex.printStackTrace();
    45. }
    46. finally
    47. {
    48. if(out != null)
    49. {
    50. try
    51. {
    52. out.close();
    53. }
    54. catch(IOException ex){}
    55. }
    56. }
    57. }
    Should work fine
     
Thread Status:
Not open for further replies.

Share This Page