001 002 import java.io.File; 003 import java.io.FileOutputStream; 004 import java.io.IOException; 005 import java.util.logging.Logger; 006 import java.io.FileInputStream; 007 import java.util.Map; 008 import java.util.Properties; 009 010 /** 011 * Used for accessing and creating .[properties] files, reads them as utf-8, saves as utf-8. 012 * Internationalization is key importance especially for character codes. 013 * 014 * @author Nijikokun 015 * @version 1.0.4, %G% 016 */ 017 public final class PropertiesFile { 018 019 private static final Logger log = Logger.getLogger("Minecraft"); 020 private String fileName; 021 private Properties props = new Properties(); 022 // private List<String> lines = new ArrayList<String>(); 023 // private Map<String, String> props = new HashMap<String, String>(); 024 025 /** 026 * Creates or opens a properties file using specified filename 027 * 028 * @param fileName 029 */ 030 public PropertiesFile(String fileName) { 031 this.fileName = fileName; 032 033 File file = new File(fileName); 034 035 try { 036 if (file.exists()) { 037 load(); 038 } else { 039 save(); 040 } 041 } catch (IOException ex) { 042 log.severe("[PropertiesFile] Unable to load " + fileName + "!"); 043 } 044 } 045 046 /** 047 * The loader for property files, it reads the file as UTF8 or converts the string into UTF8. 048 * Used for simple runthrough's, loading, or reloading of the file. 049 * 050 * @throws IOException 051 */ 052 public void load() throws IOException { 053 props.load(new FileInputStream(fileName)); 054 } 055 056 /** 057 * Writes out the <code>key=value</code> properties that were changed into 058 * a .[properties] file in UTF8. 059 */ 060 public void save() { 061 try { 062 props.store(new FileOutputStream(fileName), null); 063 }catch(IOException ex) { 064 } 065 } 066 067 /** 068 * Returns a Map of all <code>key=value</code> properties in the file as <code><key (java.lang.String), value (java.lang.String)></code> 069 * <br /><br /> 070 * Example: 071 * <blockquote><pre> 072 * PropertiesFile settings = new PropertiesFile("settings.properties"); 073 * Map<String, String> mappedSettings; 074 * 075 * try { 076 * mappedSettings = settings.returnMap(); 077 * } catch (Exception ex) { 078 * log.info("Failed mapping settings.properties"); 079 * } 080 * </pre></blockquote> 081 * 082 * @return <code>map</code> - Simple Map HashMap of the entire <code>key=value</code> as <code><key (java.lang.String), value (java.lang.String)></code> 083 * @throws Exception If the properties file doesn't exist. 084 */ 085 public Map<String, String> returnMap() throws Exception { 086 return (Map<String, String>) props.clone(); 087 } 088 089 /** 090 * Checks to see if the .[properties] file contains the given <code>key</code>. 091 * 092 * @param var The key we are going to be checking the existance of. 093 * @return <code>Boolean</code> - True if the <code>key</code> exists, false if it cannot be found. 094 */ 095 public boolean containsKey(String var) { 096 return props.containsKey(var); 097 } 098 099 /** 100 * Checks to see if this <code>key</code> exists in the .[properties] file. 101 * 102 * @param var The key we are grabbing the value of. 103 * @return <code>java.lang.String</code> - True if the <code>key</code> exists, false if it cannot be found. 104 */ 105 public String getProperty(String var) { 106 return (String)props.getProperty(var); 107 } 108 109 /** 110 * Remove a key from the file if it exists. 111 * This will save() which will invoke a load() on the file. 112 * 113 * @see #save() 114 * @param var The <code>key</code> that will be removed from the file 115 */ 116 public void removeKey(String var) { 117 if (this.props.containsKey(var)) { 118 this.props.remove(var); 119 save(); 120 } 121 } 122 123 /** 124 * Checks the existance of a <code>key</code>. 125 * 126 * @see #containsKey(java.lang.String) 127 * @param key The <code>key</code> in question of existance. 128 * @return <code>Boolean</code> - True for existance, false for <code>key</code> found. 129 */ 130 public boolean keyExists(String key) { 131 return containsKey(key); 132 } 133 134 /** 135 * Returns the value of the <code>key</code> given as a <code>String</code>, 136 * however we do not set a string if no <code>key</code> is found. 137 * 138 * @see #getProperty(java.lang.String) 139 * @param key The <code>key</code> we will retrieve the property from, if no <code>key</code> is found default to "" or empty. 140 */ 141 public String getString(String key) { 142 if (this.containsKey(key)) { 143 return this.getProperty(key); 144 } 145 146 return ""; 147 } 148 149 /** 150 * Returns the value of the <code>key</code> given as a <code>String</code>. 151 * If it is not found, it will invoke saving the default <code>value</code> to the properties file. 152 * 153 * @see #setString(java.lang.String, java.lang.String) 154 * @see #getProperty(java.lang.String) 155 * @param key The key that we will be grabbing the value from, if no value is found set and return <code>value</code> 156 * @param value The default value that we will be setting if no prior <code>key</code> is found. 157 * @return java.lang.String Either we will return the default value or a prior existing value depending on existance. 158 */ 159 public String getString(String key, String value) { 160 if (this.containsKey(key)) { 161 return this.getProperty(key); 162 } 163 164 setString(key, value); 165 return value; 166 } 167 168 /** 169 * Save the value given as a <code>String</code> on the specified key. 170 * 171 * @see #save() 172 * @param key The <code>key</code> that we will be addressing the <code>value</code> to. 173 * @param value The <code>value</code> we will be setting inside the <code>.[properties]</code> file. 174 */ 175 public void setString(String key, String value) { 176 props.put(key, value); 177 save(); 178 } 179 180 /** 181 * Returns the value of the <code>key</code> given in a Integer, 182 * however we do not set a string if no <code>key</code> is found. 183 * 184 * @see #getProperty(String var) 185 * @param key The <code>key</code> we will retrieve the property from, if no <code>key</code> is found default to 0 186 */ 187 public int getInt(String key) { 188 if (this.containsKey(key)) { 189 return Integer.parseInt(this.getProperty(key)); 190 } 191 192 return 0; 193 } 194 195 /** 196 * Returns the int value of a key 197 * 198 * @see #setInt(String key, int value) 199 * @param key The key that we will be grabbing the value from, if no value is found set and return <code>value</code> 200 * @param value The default value that we will be setting if no prior <code>key</code> is found. 201 * @return <code>Integer</code> - Either we will return the default value or a prior existing value depending on existance. 202 */ 203 public int getInt(String key, int value) { 204 if (this.containsKey(key)) { 205 return Integer.parseInt(this.getProperty(key)); 206 } 207 208 setInt(key, value); 209 return value; 210 211 } 212 213 /** 214 * Save the value given as a <code>int</code> on the specified key. 215 * 216 * @see #save() 217 * @param key The <code>key</code> that we will be addressing the <code>value</code> to. 218 * @param value The <code>value</code> we will be setting inside the <code>.[properties]</code> file. 219 */ 220 public void setInt(String key, int value) { 221 props.put(key, String.valueOf(value)); 222 223 save(); 224 } 225 226 /** 227 * Returns the value of the <code>key</code> given in a Double, 228 * however we do not set a string if no <code>key</code> is found. 229 * 230 * @see #getProperty(String var) 231 * @param key The <code>key</code> we will retrieve the property from, if no <code>key</code> is found default to 0.0 232 */ 233 public double getDouble(String key) { 234 if (this.containsKey(key)) { 235 return Double.parseDouble(this.getProperty(key)); 236 } 237 238 return 0; 239 } 240 241 /** 242 * Returns the double value of a key 243 * 244 * @see #setDouble(String key, double value) 245 * @param key The key that we will be grabbing the value from, if no value is found set and return <code>value</code> 246 * @param value The default value that we will be setting if no prior <code>key</code> is found. 247 * @return <code>Double</code> - Either we will return the default value or a prior existing value depending on existance. 248 */ 249 public double getDouble(String key, double value) { 250 if (this.containsKey(key)) { 251 return Double.parseDouble(this.getProperty(key)); 252 } 253 254 setDouble(key, value); 255 return value; 256 } 257 258 /** 259 * Save the value given as a <code>double</code> on the specified key. 260 * 261 * @see #save() 262 * @param key The <code>key</code> that we will be addressing the <code>value</code> to. 263 * @param value The <code>value</code> we will be setting inside the <code>.[properties]</code> file. 264 */ 265 public void setDouble(String key, double value) { 266 props.put(key, String.valueOf(value)); 267 268 save(); 269 } 270 271 /** 272 * Returns the value of the <code>key</code> given in a Long, 273 * however we do not set a string if no <code>key</code> is found. 274 * 275 * @see #getProperty(String var) 276 * @param key The <code>key</code> we will retrieve the property from, if no <code>key</code> is found default to 0L 277 */ 278 public long getLong(String key) { 279 if (this.containsKey(key)) { 280 return Long.parseLong(this.getProperty(key)); 281 } 282 283 return 0; 284 } 285 286 /** 287 * Returns the long value of a key 288 * 289 * @see #setLong(String key, long value) 290 * @param key The key that we will be grabbing the value from, if no value is found set and return <code>value</code> 291 * @param value The default value that we will be setting if no prior <code>key</code> is found. 292 * @return <code>Long</code> - Either we will return the default value or a prior existing value depending on existance. 293 */ 294 public long getLong(String key, long value) { 295 if (this.containsKey(key)) { 296 return Long.parseLong(this.getProperty(key)); 297 } 298 299 setLong(key, value); 300 return value; 301 } 302 303 /** 304 * Save the value given as a <code>long</code> on the specified key. 305 * 306 * @see #save() 307 * @param key The <code>key</code> that we will be addressing the <code>value</code> to. 308 * @param value The <code>value</code> we will be setting inside the <code>.[properties]</code> file. 309 */ 310 public void setLong(String key, long value) { 311 props.put(key, String.valueOf(value)); 312 313 save(); 314 } 315 316 /** 317 * Returns the value of the <code>key</code> given in a Boolean, 318 * however we do not set a string if no <code>key</code> is found. 319 * 320 * @see #getProperty(String var) 321 * @param key The <code>key</code> we will retrieve the property from, if no <code>key</code> is found default to false 322 */ 323 public boolean getBoolean(String key) { 324 if (this.containsKey(key)) { 325 return Boolean.parseBoolean(this.getProperty(key)); 326 } 327 328 return false; 329 } 330 331 /** 332 * Returns the boolean value of a key 333 * 334 * @see #setBoolean(String key, boolean value) 335 * @param key The key that we will be grabbing the value from, if no value is found set and return <code>value</code> 336 * @param value The default value that we will be setting if no prior <code>key</code> is found. 337 * @return <code>Boolean</code> - Either we will return the default value or a prior existing value depending on existance. 338 */ 339 public boolean getBoolean(String key, boolean value) { 340 if (this.containsKey(key)) { 341 return Boolean.parseBoolean(this.getProperty(key)); 342 } 343 344 setBoolean(key, value); 345 return value; 346 } 347 348 /** 349 * Save the value given as a <code>boolean</code> on the specified key. 350 * 351 * @see #save() 352 * @param key The <code>key</code> that we will be addressing the <code>value</code> to. 353 * @param value The <code>value</code> we will be setting inside the <code>.[properties]</code> file. 354 */ 355 public void setBoolean(String key, boolean value) { 356 props.put(key, String.valueOf(value)); 357 358 save(); 359 } 360 }