Properties类的层次结构
properties文件是一个文本文件,注释格式为#XXX;正文配置为A=B。
void java.util.Properties.load(InputStream inStream) throws IOException
从输入字节流中读取一个属性列表。
Object java.util.Hashtable.put(Object key, Object value)
指定新映射。返回原key映射的值。若不存在返回null。
String java.util.Properties.getProperty(String key)
java.lang.Object
java.util.Dictionary<K,V>
java.util.Hashtable<Object,Object>
java.util.Properties
properties文件是一个文本文件,注释格式为#XXX;正文配置为A=B。
void java.util.Properties.load(InputStream inStream) throws IOException
从输入字节流中读取一个属性列表。
Object java.util.Hashtable.put(Object key, Object value)
指定新映射。返回原key映射的值。若不存在返回null。
String java.util.Properties.getProperty(String key)
返回key对应的value。
例子
import java.io.*;
import java.util.Date;
import java.util.Properties;
public class TestProperties {
public static void main(String args[]) throws IOException {
test();
}
public static void test() throws IOException {
InputStream is = new FileInputStream("D:\\pp.properties");
Properties prop = new Properties();
prop.load(is);
//循环输出配置信息
for (Object key : prop.keySet()) {
System.out.println(key + "=" + prop.get(key));
}
is.close();
//修改Properties对象,并持久化到一个文件
// prop.put("c", 5);
// OutputStream os = new FileOutputStream("D:\\pp.properties");
// String str="saved on "+ new Date();
// prop.store(os,str);
// os.close();
}
}
/*b=2
a=1*/
文件与资源
有时我们会把配置文件也放到jar包内,此时用File类读取就不行了,需要用下列函数:
InputStream java.lang.ClassLoader.getResourceAsStream(String name)
使用它还有一个好处,在开发调试时与最终运行时都不需要再改读配置的路径啦。
封装好的config类
单例模式。
package com.yichudu.yichuutil;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class Config {
private static Config instance = null;
private Properties properties = new Properties();
private boolean refreshEnable;
private int reFreshInterval=1;
private long lastRefreshTimestamp=0;
private String path;
public void enableRefresh(int reFreshIntervalBySecond){
refreshEnable=true;
reFreshInterval=reFreshIntervalBySecond;
}
private Config() {
}
synchronized public static Config getInstance() {
if (instance == null)
instance = new Config();
return instance;
}
// public void loadResource(String path) throws IOException {
// InputStream is = Config.class.getClassLoader()
// .getResourceAsStream(path);
// InputStreamReader isr = new InputStreamReader(is, "UTF-8");
// properties.load(isr);
// }
public void loadFile(String path) throws IOException {
this.path=path;
properties.load(new FileReader(path));
}
public String get(String key) {
try{
if(refreshEnable)
refreshCheck();
}catch(Exception e){
return properties.getProperty(key);
}
return properties.getProperty(key);
}
public String get(String key, String defaultValue) {
try{
if(refreshEnable)
refreshCheck();
}catch(Exception e){
return properties.getProperty(key) == null ? defaultValue : properties
.getProperty(key);
}
return properties.getProperty(key) == null ? defaultValue : properties
.getProperty(key);
}
void refreshCheck() throws IOException{
if(System.currentTimeMillis()-lastRefreshTimestamp>reFreshInterval*1000){
lastRefreshTimestamp=System.currentTimeMillis();
loadFile(path);
}
}
@Override
public String toString() {
return properties.toString();
}
}