1 Properties类
1.1 认识properties文件1.2 了解Properties类1.3 properties文件与Properties类的关系1.4 使用Properties类
1.4.1 操作properties1.4.2 操作XML文件1.4.3 java读取properties文件的九种方法
1 Properties类由于java.util.Properties类也是属于继承Map的,所以就归类到集合这章了
1.1 认识properties文件了解并认识properties文件
- properties文件是一个文本文件properties文件的语法有两种,一种是注释,一种属性配置。
注 释:前面加上#号
属性配置:以键=值的方式书写一个属性的配置信息。properties文件的一个属性配置信息值可以换行,但键不可以换行。值换行用表示properties的属性配置键值前后的空格在解析时候会被忽略properties文件可以只有键而没有值。也可以仅有键和等号而没有值,但无论如何一个属性配置不能没有键
例如,下面一个properties文件:
#正确的properties配置文件
aaa=1
11
b
bb = 222
#格式良好的properties文件
aaa=111
bbb=222
1.2 了解Properties类
Properties类的层次结构
java.lang.Object java.util.Dictionaryjava.util.Hashtable
从层次机构看,Properties类实现了Map接口,因为HashTable实现了Map接口,因此Properties类本质上是一种简单的Map容器。
实际上,Properties类本身表示了对一种Map结构的操作。properties文件本身就表示了一个键值对的集合。因此,Properties类属于集合容器的家族,在使用前应该创建一个Properties的容器,实际上就是创建一个默认不带参数的 Properties对象。以后通过别的方式给里面添加键值对。
通过properties文件可以填充Properties类
也可以通过xml文件来填充Properties类。
可以通过绝对路径方式加载Properties文件信息,也可以使用相对路径加载
public static void test1() throws IOException {
InputStream is = PropertiesDemo.class.getResourceAsStream("/log4j.properties");
Properties p = new Properties();
p.load(is);
for (Object key : p.keySet()) {
System.out.println(key + "=" + p.get(key));
}
}
public static void main(String[] args) throws Exception {
test1();
}
1.4.2 操作XML文件
public static void testProperties() throws IOException {
//将properties文件加载到输入字节流中
InputStream is = new ClassPathResource("logback.xml").getInputStream();
//创建一个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("ttt.xml");
OutputStream os2 = new FileOutputStream("ttt.properties");
//从Properties对象导出导出到xml
prop.storeToXML(os1, "我从properties导出的XML配置文件");
//从Properties对象导出properties文件
prop.store(os2, "我从properties导出的XML配置文件");
is.close();
os1.close();
os2.close();
}
public static void main(String[] args) throws Exception {
testProperties();
}
1.4.3 java读取properties文件的九种方法
假如读取的是log4j.properties
java读取properties文件的九种方法:
- 使用java.util.properties类的load()方法
示例:
InputStream in = new BufferedInputStream(new FileInputStream("绝对路径"))
properties p = new properties()
p.load(in)
- 使用class变量的getResourceAsStream()方法
示例:
Inputstream in = PropertiesDemo.class.getResourceAsStream("/log4j.properties")
properties p = new properties()
p.load(in)
- 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例:
Inputstream in = PropertiesDemo.class.getClassLoader().getResourceAsStream("log4j.properties")
properties p = new properties()
p.load(in)
- 使用当前类加载器Thread.currentThread().getContextClassLoader()的getResourceAsStream方法
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("log4j.properties");
properties p = new properties()
p.load(in)
- 使用java.lang.ClassLoader类的getsystemresourceasstream()静态方法
示例:
inputstream in = ClassLoader.getSystemResourceAsStream("log4j.properties")
properties p = new properties()
p.load(in)
- 使用ClassPathResource的构造方法加载读取
ClassPathResource classPathResource = new ClassPathResource("log4j.properties");
InputStream is = classPathResource.getInputStream();
- 使用spring自带的ResourceUtils加载
File file = ResourceUtils.getFile("classpath:log4j.properties");
InputStream is = new FileInputStream(file);
- 使用java.util.ResourceBundle类的getBundle()方法,只用输入配置文件名字不用加后缀
示例:
ResourceBundle rb = ResourceBundle.getBundle("log4j",Locale.getdefault())
Enumeration keys = rb.getKeys();
while (keys.hasMoreElements()){
String s = keys.nextElement();
System.out.println(s);
}
- 使用java.util.PropertyResourceBundle类的构造函数
示例:
Inputstream in = new Bufferedinputstream(new fileinputstream("绝对路径"))
ResourceBundle rb = new PropertyResourceBundle(in)



