import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
public class ConfigManager {
private static ConfigManager cm = new ConfigManager();
private File file ;
private Properties props;
private ConfigManager(){
file = new File("Singleton.properties");
props = new Properties();
try {
props.load(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
synchronized public static ConfigManager getConfigManagerInstance(){
return cm;
}
public Object getConfigItem(String name, Object defaultVal){
Object val = props.getProperty(name);
if(val == null){
return defaultVal;
}
return val;
}
public boolean contains(Object value){
if(props.contains(value))
return true;
return false;
}
public static void main(String[] args) throws Exception{
ConfigManager cm = ConfigManager.getConfigManagerInstance();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
do{
System.out.println("Properties item to read:");
String line = br.readLine();
if(line == "quit"){
break;
}
System.out.println(line + " : "+ cm.getConfigItem(line, null));
}while(true);
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
public class ConfigManager {
private static ConfigManager cm = new ConfigManager();
private File file ;
private Properties props;
private ConfigManager(){
file = new File("Singleton.properties");
props = new Properties();
try {
props.load(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
synchronized public static ConfigManager getConfigManagerInstance(){
return cm;
}
public Object getConfigItem(String name, Object defaultVal){
Object val = props.getProperty(name);
if(val == null){
return defaultVal;
}
return val;
}
public boolean contains(Object value){
if(props.contains(value))
return true;
return false;
}
public static void main(String[] args) throws Exception{
ConfigManager cm = ConfigManager.getConfigManagerInstance();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
do{
System.out.println("Properties item to read:");
String line = br.readLine();
if(line == "quit"){
break;
}
System.out.println(line + " : "+ cm.getConfigItem(line, null));
}while(true);
}
}
本文介绍了一个简单的配置管理器实现方式,使用Java编写,通过单例模式管理配置文件的加载和读取。该类能够从指定的属性文件中加载配置项,并提供方法来获取配置项的值。
1502

被折叠的 条评论
为什么被折叠?



