I created 2 methods 1 for loading string from .propertie file and 1 for savng string to propertie file:
public void SavePropertie(String file, String key, String value){ Properties properties = new Properties(); File xfile = new File("plugins/UnixRPG/" + file + ".properties"); if (xfile.exists()){ //Copying the old part. try { FileInputStream in = new FileInputStream("plugins/UnixRPG/" + file + ".properties"); properties.load(in); in.close(); } catch (IOException e) {} @SuppressWarnings("rawtypes") Enumeration em = properties.keys(); while(em.hasMoreElements()){ String str = (String) em.nextElement(); if (str != key){ properties.setProperty(str, LoadPropertie(file, str)); } } } //Adding new properties line. properties.setProperty(key, value); try { FileOutputStream out = new FileOutputStream("plugins/UnixRPG/" + file + ".properties"); properties.store(out, key + ": " + value); out.close(); out.flush(); } catch (IOException e) {} properties.clear(); } public String LoadPropertie(String file, String key){ Properties properties = new Properties(); String value = null; FileInputStream in = null; try { properties.load(in = new FileInputStream("plugins/UnixRPG/" + file + ".properties")); value = properties.getProperty(key); in.close(); } catch (IOException e) {} properties.clear(); return value; }
When i load/save Strings from my .propertie file it seems it dose not close the file properly. When my program tryes to delete the .propertie file it fails every time.
This is the part of the code where i need to delete the file:
public boolean onRemove(Player player, Location location){ World world = location.getWorld(); int x = (int) location.getX(); int y = (int) location.getY(); int z = (int) location.getZ(); File file = new File("plugins/UnixRPG/signs/toggleblocks/" + world.getName() + "," + Integer.toString(x) + "," + Integer.toString(y) + "," + Integer.toString(z) + ".propertie"); boolean sucsess = file.delete(); if (sucsess == true){ Material1.remove(location); Material2.remove(location); FirstBlockLocation.remove(location); LastBlockLocation.remove(location); return true; } else { player.sendRawMessage(ChatColor.RED + "ERROR: Failled deleting the toggleblocks file."); return false; } }
It says "ERROR: Failled deleting the toggleblocks file." every time.