package com.itcast;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Properties;
public class ConfigUtil {
public static final String DEFAULT_CONFIGNAME="viewConfig.properties";
private static Hashtable property = new Hashtable();
private static Properties defPro=loadProperty(DEFAULT_CONFIGNAME);
static
{
if(defPro==null)
{
throw new RuntimeException("Can't Load " +DEFAULT_CONFIGNAME);
}
}
public static String getProperty(String key)
{
String str = defPro.getProperty(key);
if (str != null)
{
return str.trim();
}
else
{
return null;
}
}
private static Properties loadProperty(String filename)
{
try
{
Properties filePro = new Properties();
filePro.load(new ConfigUtil().getClass().getClassLoader().getResourceAsStream(filename));
property.put(filename,filePro);
return filePro;
}
catch (Exception ex)
{
return null;
}
}
}
test:
private static Hashtable property = new Hashtable();
private static Properties defPro=loadProperty(DEFAULT_CONFIGNAME);
static
{
if(defPro==null)
{
throw new RuntimeException("Can't Load " +DEFAULT_CONFIGNAME);
}
}
public static String getProperty(String key)
{
String str = defPro.getProperty(key);
if (str != null)
{
return str.trim();
}
else
{
return null;
}
}
private static Properties loadProperty(String filename)
{
try
{
Properties filePro = new Properties();
filePro.load(new ConfigUtil().getClass().getClassLoader().getResourceAsStream(filename));
property.put(filename,filePro);
return filePro;
}
catch (Exception ex)
{
return null;
}
}
}
test:
package com.itcast.test;
import com.itcast.ConfigUtil;
public class TestInfo {
/**
* @param args
*/
public static void main(String[] args) {
String ss= ConfigUtil.getProperty("broadCastSendHostIp");
System.out.println(ss);
}
* @param args
*/
public static void main(String[] args) {
String ss= ConfigUtil.getProperty("broadCastSendHostIp");
System.out.println(ss);
}
}