Child: [r4] (diff)

Download this file

Settings.java    145 lines (117 with data), 3.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* A class to save and load settings from disk using XML and simple properties
* class built-in support for XML
*/
package utils;
import java.io.*;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Nuno Brito, 25th of December 2012 in Kirchberg, Austria.
*/
public class Settings {
// 2013-MAY-11 psc create properties in constructor
private Properties properties;
private File settingsFile;
private String comment = "";
public Enumeration getAllKeys(){
return properties.keys();
}
/**
* Creates a new settings object where you can keep pairs of key-values that
* will be kept on disk
*
* @param settingsFile The XML file where all settings will be kept
*/
public Settings(File settingsFile, String comment){
properties = new Properties();
this.settingsFile = settingsFile;
this.comment = comment;
if(settingsFile.exists()){
load();
}
}
/**
* Writes a key in the settings, each written key is automatically written
* back to the corresponding XML file.
* @param key
* @param value
*/
public void write(String key, String value){
properties.setProperty(key, value);
save();
}
/**
* Removes a given key from the settings
* @param key
*/
public void delete(String key){
properties.remove(key);
save();
}
/**
* Simply saves all contents of this object at an XML file on disk
*/
private boolean save(){
OutputStream out;
try {
out = new FileOutputStream(settingsFile);
properties.storeToXML(out, comment);
// 2013-MAY-11 psc close it!
out.close();
} catch (IOException ex) {
Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
return true;
}
/**
* If a given XML file with settings already exist, then try to load it up
* and get the values back
*/
private void load(){
InputStream in;
try {
in = new FileInputStream(settingsFile);
properties.loadFromXML(in);
// 2013-MAY-11 psc close it!
in.close();
} catch (IOException ex) {}
}
/**
* Returns the value of a given key, if no key exists then the return value
* is empty.
*/
public String read(String key){
return properties.getProperty(key);
}
/**
* Returns the value of a given key. If the key does not exist then it will
* return the alternative value instead.
* @param key
* @param alternative
*/
public String read(String key, String alternative){
return properties.getProperty(key, alternative);
}
public Boolean hasKey(String key){
return properties.containsKey(key);
}
public static void main(String[] args) {
Settings set = new Settings(new File("test1234.xml"), "testing the "
+ "settings to see how they work");
set.write("test", "1234");
set.write("test1", "1234aas");
set.write("test", "1234343");
String test = set.read("test");
if(test.isEmpty()==false) {
System.out.println(test);
}
}
public Properties getProperties() {
return properties;
}
}