?? configmanager.java
字號:
/**
*
*/
package com.justin.config;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.io.*;
import com.justin.log.*;
/**
* @author Justin
*
* 單例模式實現,此類用來讀取并存放程序的配置信息
*/
public class ConfigManager {
private static Properties prop;
private static ConfigManager config = null;
public synchronized static ConfigManager getInstance(){
if (config == null){
config = new ConfigManager();
}
return config;
}
private ConfigManager(){
try {
prop = new Properties();
//設置默認屬性
prop.setProperty("AppRoot", (String)System.getProperty("user.dir"));
prop.setProperty("DetectInterval", "3000");
prop.setProperty("DetectDir","C:\\Data");
//加載用戶定制的配制文件
prop.load(new FileInputStream((String)System.getProperty("user.dir")+"\\config.properties"));
//輸出默認屬性及配置文件的內容
prop.list(System.out);
} catch (IOException ex) {
System.out.println("No Config File");
}
}
//在用戶界面顯示所有的屬性及值
public synchronized void list(LogServer log){
log.showMessage("-- listing properties --");
Hashtable<String, String> h = new Hashtable<String, String>();
for (Enumeration e = prop.keys() ; e.hasMoreElements() ;) {
String key = (String)e.nextElement();
h.put(key, (String)prop.get(key));
}
for (Enumeration e = h.keys() ; e.hasMoreElements() ;) {
String key = (String)e.nextElement();
String val = (String)h.get(key);
if (val.length() > 40) {
val = val.substring(0, 37) + "...";
}
log.showMessage(key + "=" + val);
}
}
//在輸出流中顯示所有屬性及值
public synchronized void list(PrintStream out){
prop.list(out);
}
//返回監測間隔屬性值
public synchronized int getDetectInterval(){
int interval = 2000;
try{
interval = Integer.parseInt(prop.getProperty("DetectInterval"));
}catch(NumberFormatException ex){
System.out.println("DetectInterval數據類型錯誤(整型)");
}
return interval;
}
//返回監測目錄文件夾屬性值
public synchronized String getDetectDir(){
return prop.getProperty("DetectDir");
}
//返回應用程序根路徑屬性值
public synchronized String getAppRoot(){
return prop.getProperty("AppRoot");
}
//返回監測文件類型屬性值
public synchronized String getDetectExt(){
return prop.getProperty("DetectExt");
}
//返回是否處于Debug狀態
public synchronized boolean isDebug(){
boolean isDebug = false;
try{
isDebug = Boolean.valueOf(prop.getProperty("Debug")).booleanValue();
}catch(NumberFormatException ex){
System.out.println("Debug數據類型錯誤(false/true)");
}
return isDebug;
}
public synchronized String getBackupDir(){
return prop.getProperty("BackupDir");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -