java.util.Properties 和 ResourceBundle

来源:互联网 发布:如何利用蜂窝网络定位 编辑:程序博客网 时间:2024/05/22 00:23

1. 为什么要使用.properties文件?

      在程序中,有时需要很多字符串信息,例如mysql.port=3306; mysql.server=127.0.0.1等。如果这些信息直接写死在程序中,当改变这些字符串时必须重新编译程序。因此常将跟程序运行无关的数据写在配置文件中。xml文件常用来存储结构化数据,而简单的配置信息通常存储在properties文件中。

   一句话,利用properties文件解决程序硬编码问题。

2. .properties文件格式

  •   properties文件的语法有两种,一种是注释,一种属性配置。注    释:前面加上#号。 属性配置:以“键=值”的方式书写一个属性的配置信息。
  •   properties文件的一个属性配置信息值可以换行,但键不可以换行。值换行用“\”表示。
  •  properties的属性配置键值前后的空格在解析时候会被忽略。
  • properties文件可以只有键而没有值。也可以仅有键和等号而没有值,但无论如何一个属性配置不能没有键。
例如,下面一个properties文件:
#正确的properties配置文件 
key1=hel\ 
    lo
key 
key2    =     goodbye
 
#格式良好的properties文件 
key1=hello
key2=goodbye

3. Properties类加载.properties文件内容和持久化

public class PropertiesDemo {    public static void main(String[] args) throws IOException {        // 将properties文件加载到inputstream中        String inFilePath = "/home/fanzj/work/my-app/src/main/java/resource.properties";        InputStream in = new FileInputStream(inFilePath);        // 从inputstream中将内容加载到Properties容器中        Properties pro = new Properties();        pro.load(in);        // 新增一对KV        pro.setProperty("key3", "test_add_KV");        // 循环输出properties文件中的信息        for (Object key : pro.keySet()) {            System.out.println(key + " : " + pro.getProperty(key.toString()));        }        // 从Properties对象导出导出到文件中        String outXmlFilePath = "/home/fanzj/work/my-app/src/main/java/resourceOut.xml";        String outProFilePath = "/home/fanzj/work/my-app/src/main/java/resourceOut.properties";        OutputStream outXml = new FileOutputStream(outXmlFilePath);        pro.storeToXML(outXml, "从Properties对象导出到xml文件中");        OutputStream outPro = new FileOutputStream(outProFilePath);        pro.store(outPro, "从Properties对象导出到.properties文件中");        // 关闭流        in.close();        outXml.close();         outPro.close();   }}

控制台 输出内容:
key3 : test_add_KVkey2 : goodbyekey1 : hello

文件resourceOut.xml内容:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"><properties><comment>从Properties对象导出到xml文件中</comment><entry key="key3">test_add_KV</entry><entry key="key2">goodbye</entry><entry key="key1">hello</entry></properties>

文件resourceOut.properties内容:

#\u4ECEProperties\u5BF9\u8C61\u5BFC\u51FA\u5230.properties\u6587\u4EF6\u4E2D#Sun Apr 14 16:40:12 CST 2013key3=test_add_KVkey2=goodbyekey1=hello

4. Properties类源码

5. 遇到国际化(编码)问题 用类ResourceBundle代替类Properties

public class ResourceBundleDemo {    public static void main(String[] args) throws IOException {        Locale locale1 = new Locale("zh", "CN");        ResourceBundle res = ResourceBundle.getBundle("resource", locale1);        // 循环输出properties文件中的信息        for (Object key : res.keySet()) {            System.out.println(key + " : " + res.getString(key.toString()));        }    }}

控制台输出:
key2 : 再见
key1 : 你好

文件resource.properties内容为
key1=hello
key2=goodbye

文件resource_zh_CN.properties内容为
key1=\u4f60\u597d
key2=\u518d\u89c1

问题: 如何得到resource_zh_CN.properties文件?
Answer: 
第一步:建立文件resource_zh.properties,内容为
key1 = 你好
key2 =再见
第二步:利用命令 native2ascii resource_zh.properties resource_zh_CN.properties
总结:

ResourceBundleDemo可以根据当前软件环境,决定是显示hello还是你好。类ResourceBundle提供软件国际化的捷径。通过此类,可以使您所编写的程序可以:

  • 轻松地本地化或翻译成不同的语言
  •  一次处理多个语言环境
  •  以后可以轻松地进行修改,支持更多的语言环境

 说的简单点,这个类的作用就是读取资源属性文件(properties),然后根据.properties文件的名称信息例如resource_zh_CN.properties(本地化信息),匹配当前系统的国别语言信息(也可以程序指定),然后获取相应的properties文件的内容。

6. 参考资料

【1】 国际化properties文件问题 http://lavasoft.blog.51cto.com/62575/184605

【2】 properties文件格式 http://trans.blog.51cto.com/503170/110227/