日期:2014-05-20 浏览次数:20818 次
public class Utils {
private static Properties props = null;
private static final String PATH = "/config.properties";
/**
* 得到config.properties配置文件中的所有配置属性
*
* <A href="http://home.51cto.com/index.php?s=/space/34010" target=_blank>@return</A> Properties对象
*/
public static Properties getConfig() {
if (null == props) {
props = new Properties();
//InputStream in = Utils.class.getResourceAsStream(PATH);
InputStream in;
try {
in = new FileInputStream(Utils.class.getClass()
.getClassLoader().getResource(PATH).getPath());
props.load(in);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return props;
}
/**
* 获得config.properties配置文件的属性值
* */
public static String getValue(String name) {
if (null == props)
getConfig();
return props.getProperty(name);
}
/**
* 设置config.properties配置文件的属性
* */
public static void setConfig(String name, String value) {
if (props == null)
getConfig();
props.setProperty(name, value);
// saveConfig();
}
/**
* 保存数据
*/
public static void saveConfig() {
try {
FileOutputStream out = new FileOutputStream(new File(PATH));
props.store(out, null);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}