您的位置:首页 > 编程语言 > Java开发

【知识积累】java.util.Properties类 操作

2017-04-07 13:40 316 查看
一、认识properties文件 1、properties文件是一个文本文件2、properties文件的语法有两种,一种是注释,一种属性配置。 注    释:前面加上#号 属性配置:以“键=值”的方式书写一个属性的配置信息。3、properties文件的一个属性配置信息值可以换行,但键不可以换行。值换行用“\”表示。4、properties的属性配置键值前后的空格在解析时候会被忽略。5、properties文件可以只有键而没有值。也可以仅有键和等号而没有值,但无论如何一个属性配置不能没有键。  例如,下面一个properties文件:#正确的properties配置文件 
aaa=1\ 
    11 

bb    =     222 #格式良好的properties文件 
aaa=111 
bbb=222 二、解读java.util.Properties类 1、Properties类的层次结构java.lang.Object  java.util.Dictionary<K,V>      java.util.Hashtable<Object,Object>          java.util.Properties 从层次机构看,Properties类实现了Map接口,因为HashTable实现了Map接口,因此Properties类本质上是一种简单的Map容器。实际上,Properties类本身表示了对一种Map结构的操作。properties文件本身就表示了一个“键值对”的集合。因此,Properties类属于集合容器的家族,在使用前应该创建一个Properties的容器,实际上就是创建一个默认不带参数的Properties对象。以后通过别的方式给里面添加“键值对”。 2、properties文件与Properties类的关系通过properties文件可以填充Properties类。也可以通过xml文件来填充Properties类。可以通过绝对路径方式加载Properties文件信息,也可以使用相对路径加载。 三、实践 1、以绝对相对路径方式加载properties文件信息。2、将Properties对象持久化到一个properties文件或者一个xml文件中。3、修改并持久化properties文件。 测试代码: 测试的properties文件:#格式良好的properties文件 
aaa=111 
bbb=222 测试类:package stu; 

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)); 
                } 
        } 
}  运行结果:------------testProperties------------- 
bbb=222 
aaa=111 
我从导出的xml加载配置文件信息! 
bbb=222 
aaa=111 
------------test1------------- 
bbb=222 
aaa=111 

Process finished with exit code 0 

使用J2SE API读取Properties文件的六种方法

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);

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: