1。使用java.util.properties类的load()方法
示例: inputstream in = lnew bufferedinputstream(new fileinputstream(name))
properties p = new properties()
p.load(in)
2。使用java.util.resourcebundle类的getbundle()方法
示例: resourcebundle rb = resourcebundle.getbundle(name locale.getdefault())
3。使用java.util.propertyresourcebundle类的构造函数
示例: inputstream in = new bufferedinputstream(new fileinputstream(name))
resourcebundle rb = new propertyresourcebundle(in)
4。使用class变量的getresourceasstream()方法
示例: inputstream in = jproperties.class.getresourceasstream(name)
properties p = new properties()
p.load(in)
5。使用class.getclassloader()所得到的java.lang.classloader的getresourceasstream()方法
示例: inputstream in = jproperties.class.getclassloader().getresourceasstream(name)
properties p = new properties()
p.load(in)
6。使用java.lang.classloader类的getsystemresourceasstream()静态方法
示例: inputstream in = classloader.getsystemresourceasstream(name)
properties p = new properties()
p.load(in)
补充
servlet中可以使用javax.servlet.servletcontext的getresourceasstream()方法
示例:inputstream in = context.getresourceasstream(path)
properties p = new properties()
p.load(in)
aaa=111
bbb=222
import java.io.*;
import java.util.Properties;
/**
* Properties类测试
* User: xiaohui
* Date: 2008-11-4 21:04:54
*/
public class TestProperties {
public static void main(String args[]) throws IOException {
testProperties();
test1();
}
public static void testProperties() throws IOException {
System.out.println( "------------testProperties-------------");
//将properties文件加载到输入字节流中
InputStream is = new FileInputStream( "D:\\myprojects\\lession4\\src\\stu\\ttt.properties");
//创建一个Properties容器
Properties prop = new Properties();
//从流中加载properties文件信息
prop.load(is);
//循环输出配置信息
for (Object key : prop.keySet()) {
System.out.println(key + "=" + prop.get(key));
}
//定义一个输出流
OutputStream os1 = new FileOutputStream( "C:\\ttt.xml");
OutputStream os2 = new FileOutputStream( "C:\\ttt.properties");
//从Properties对象导出导出到xml
prop.storeToXML(os1, "我从properties导出的XML配置文件");
//从Properties对象导出properties文件
prop.store(os2, "我从properties导出的XML配置文件");
is.close();
os1.close();
os2.close();
//从xml加载配置信息,填充Properties容器
prop.loadFromXML( new FileInputStream( "C:\\ttt.xml"));
//循环输出配置信息
System.out.println( "我从导出的xml加载配置文件信息!");
for (Object key : prop.keySet()) {
System.out.println(key + "=" + prop.get(key));
}
//修改Properties对象,并持久化到一个文件
prop.put( "呵呵呵", "嘎嘎嘎");
OutputStream os3 = new FileOutputStream( "C:\\ttt1.xml");
prop.storeToXML(os3, "我从properties导出的XML配置文件");
os3.close();
}
/**
* 以相对路径方式加载properties文件
*
* @throws IOException
*/
public static void test1() throws IOException {
System.out.println( "------------test1-------------");
Properties p = new Properties();
p.load(TestProperties. class.getResourceAsStream( "/stu/ttt.properties"));
for (Object key : p.keySet()) {
System.out.println(key + "=" + p.get(key));
}
}
}
bbb=222
aaa=111
我从导出的xml加载配置文件信息!
bbb=222
aaa=111
------------test1-------------
bbb=222
aaa=111
Process finished with exit code 0