浅谈Java中Properties类的详细使用

目录一、Properties 类二、打印JVM参数三、打印自定义.properties文件中的值3.1、list输出到控制台用绝对路径加载3.2、propertyNames输出getClass()加载3.3、stringPropertyNames输出getClassLoader加载(推荐)四、获取值getProperties五、写入到Properties文件5.1、普通写入,中文时乱码5.2、解决乱码写入的问题六、加载和导出到xml配置文件6.1、导出到.xml配置文件storeToXML6.2、导出XML配置文件loadFromXML

一、Properties 类

Properties 类位于 java.util.Properties ,是Java 语言的配置文件所使用的类, Xxx.properties 为Java 语言常见的配置文件,如数据库的配置 jdbc.properties, 系统参数配置 system.properties。 这里,讲解一下Properties 类的具体使用。以key=value 的 键值对的形式进行存储值。 key值不能重复。

继承了Hashtable 类,以Map 的形式进行放置值, put(key,value) get(key)

主要方法:

这里只讲解一些常用的形式。

二、打印JVM参数

JVM 中可以获取Properties, 来打印输出 JVM 所了解的属性值。用list() 方法,打印到控制台。

@Testpublic void printTest(){    Properties properties=System.getProperties();    properties.list(System.out);}

常见的有:

三、打印自定义.properties文件中的值

在src 目录下,放置 jdbc.properties 文件,是数据库的配置文件。

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8

jdbc.username=root

jdbc.password=abc123

3.1、list输出到控制台用绝对路径加载

@Testpublic void name1Test(){    try{        Properties properties=new Properties();        //用的是磁盘符的绝对路径         InputStream input=new BufferedInputStream(new FileInputStream("D:\\workspace\\JavaLearn\\src\\jdbc.properties"));        properties.load(input);        properties.list(System.out);    }catch(Exception e){        e.printStackTrace();    }}

url 被截取了。

3.2、propertyNames输出getClass()加载

@Testpublic void name2Test(){    try{        Properties properties=new Properties();  // 用/文件名, / 表示根目录        InputStream input=PropertiesTest.class.getClass().getResourceAsStream("/jdbc.properties");        properties.load(input);        Enumeration<String> names=(Enumeration<String>) properties.propertyNames();        while(names.hasMoreElements()){            //这是key值            String key=names.nextElement();            String value=properties.getProperty(key);            System.out.println(key+"="+value);        }    }catch(Exception e){        e.printStackTrace();    }}

3.3、stringPropertyNames输出getClassLoader加载(推荐)

@Testpublic void name3Test(){    try{        Properties properties=new Properties();        //直接写src 类路径下的文件名        InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");        properties.load(input);                //把key值转换成set 的形式,遍历set        Set<String> names=properties.stringPropertyNames();        Iterator<String> iterator=names.iterator();        while(iterator.hasNext()){            String key=iterator.next();            String value=properties.getProperty(key);            System.out.println(key+"="+value);        }            }catch(Exception e){        e.printStackTrace();    }}

四、获取值getProperties

@Testpublic void name3Test(){    try{        Properties properties=new Properties();        InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");        properties.load(input);        //String value=properties.getProperty("jdbc.url");        String value=properties.getProperty("jdbc.url1","没有该key值");        System.out.println("输出值:"+value);            }catch(Exception e){        e.printStackTrace();    }}

输出时,getProperty() 有当前的key值,则输出Key值对应的value 值。如果没有key值,则输出 null 值。后面可以跟 default 值,如果没有该值,则输出设置的默认值。

五、写入到Properties文件

5.1、普通写入,中文时乱码

@Testpublic void writeTest(){    try{        Properties properties=new Properties();        InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");        properties.load(input);                //多添加几个值。        properties.setProperty("name","两个蝴蝶飞");        properties.setProperty("sex","男");        //properties.put("name","两个蝴蝶飞");  可以用继承Hashtable 的put 方法写入值        // properties.put("sex","男");                //将添加的值,连同以前的值一起写入 新的属性文件里面。        OutputStream out=new FileOutputStream("D:\\jdbc.properties");        properties.store(out,"填充数据");            }catch(Exception e){        e.printStackTrace();    }}

5.2、解决乱码写入的问题

在构建输入流和输出流时,指定编码格式, 编码的格式相同。 如均是 utf-8的形式。

@Testpublic void write2Test(){    try{        Properties properties=new Properties();        //用绝对路径        InputStream input=new BufferedInputStream(new FileInputStream("D:\\workspace\\JavaLearn\\src\\jdbc.properties"));        properties.load(new InputStreamReader(input,"utf-8"));            //多添加几个值。        properties.setProperty("name","两个蝴蝶飞");        properties.setProperty("sex","男");                OutputStream output=new FileOutputStream("D:\\jdbc.properties");        OutputStreamWriter out=new OutputStreamWriter(output,"utf-8");        properties.store(out,"填充数据");            }catch(Exception e){        e.printStackTrace();    }}

测试运行之后:

这样便解决了乱码的问题。

六、加载和导出到xml配置文件

6.1、导出到.xml配置文件storeToXML

将Properties 类中定义的属性,导出成 .xml 的形式.

@Testpublic void xmlWriteTest(){    try{        //处理成编码样式。        Properties properties=new Properties();                    //多添加几个值。        properties.setProperty("name","两个蝴蝶飞");        properties.setProperty("sex","男");        OutputStream output=new FileOutputStream("D:\\jdbc.xml");        //编码设置成utf-8的形式。         properties.storeToXML(output,"填充到xml","utf-8");            }catch(Exception e){        e.printStackTrace();    }}

测试结果为:

用 <entry> 节点 key为属性, 后面跟值来进行输入。可按照这种形式,继续添加。

6.2、导出XML配置文件loadFromXML

@Testpublic void xmlReadTest(){    try{        Properties properties=new Properties();        InputStream input=new BufferedInputStream(new FileInputStream("D:\\jdbc.xml"));        properties.loadFromXML(input);        properties.list(System.out);            }catch(Exception e){        e.printStackTrace();    }}

以上就是浅谈Java中Properties类的详细使用的详细内容,更多关于Java Properties的资料请关注其它相关文章!

于是渐渐开始有些伤怀。

浅谈Java中Properties类的详细使用

相关文章:

你感兴趣的文章:

标签云: