package mytools.readfile;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
/**
* This class is used to read .properties file.
*
* @author WangJunyu
*/
public class ReadProperty {
private String file = "";
private Hashtable config = new Hashtable();
public ReadProperty(String file) {
this.file = file;
}
private void readConfig() {
Properties prop = new Properties();
String key = "";
String value = "";
try {
prop.load(new FileInputStream(file));
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Enumeration emu = prop.keys();
while (emu.hasMoreElements()) {
key = (String) emu.nextElement();
value = prop.getProperty(key);
config.put(key, value);
}
}
public String get(String key) {
String value = (String) config.get(key);
if (("".equals(value)) || (value == null)) {
System.out.println("No config!");
}
return value;
}
public static void main(String[] args) {
ReadProperty read = new ReadProperty("test.properties");
read.readConfig();
System.out.println(read.get("key1"));
}
}