JAVA操作properties配置文件

11 篇文章 0 订阅
1 篇文章 0 订阅

一、简介

<1> java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式。

<2> 在properties文件中,可以用"#"来作注释

properties文件在Java编程中用到的地方很多,操作很方便。

二、Java的Properties类

属性映射(property map):是一种存储键/值对的数据结构。属性映射经常被用来存放配置信息。

它有三个特性:

  1. 键和值斗志字符串
  2. 键/值对可以很容易地写入文件或从文件读出。
  3. 用二级表存放默认值

实现属性映射的Java类被称为Properties(Java.util.Properties),此类是Java中比较重要的类,主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。

此类是线程安全的:多个线程可以共享单个 Properties 对象而无需进行外部同步。

Properties类继承自Hashtable,如下:

 

构造方法:

  • Properties() 创建一个无默认值的空属性列表。
  • Properties(Properties defaults) 创建一个带有指定默认值的空属性列表。

它提供了几个主要的方法:

1. getProperty ( String key):用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。

2. load ( InputStream inStream):从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。

3. setProperty ( String key, String value) :调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。

4. store ( OutputStream out, String comments):以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。

5. clear ():清除所有装载的 键 - 值对。该方法在基类中提供。

因为 Properties 继承于 Hashtable,所以可对 Properties 对象应用 put 和 putAll 方法。但不建议使用这两个方法,因为它们允许调用者插入其键或值不是 String 的项。相反,应该使用 setProperty 方法。

如果在“不安全”的 Properties 对象(即包含非 String 的键或值)上调用 store 或 save 方法,则该调用将失败。

类似地,如果在“不安全”的 Properties 对象(即包含非 String 的键)上调用 propertyNames 或 list 方法,则该调用将失败。

 

Properties类提供默认值的两种机制:
<1>在试图获得字符串值时制定默认值。(当键值不存在的时候,就会自动时用它)

 

String title=settings.getProperty("title","Default title");

 

 

 

 

<2>如果觉得每次调用都指定默认值太麻烦,那么就可以将所有的默认值放在一个二级属性映射中,并在主映射的构造器中提供映射。且用它来构造查询表。

 

 
  1. Properties defaultSettings=new properties();

  2. defaultSettings.setProperty("width","300");

  3. defaultSettings.setProperty("height","200");

  4. ...

  5. Properties settings=new properties(defaultSettings);

  6.  

注意:属性映射是没有层次结构的简单表。但是可以简单的使用java中的包命名方式来简单伪装一下层次结构。如果要存储复杂的配置信息,就应该使用Preferences类。

三、Java读取Properties文件的方法

<1>Java虚拟机(JVM)有自己的系统配置文件(system.properties),我们可以通过下面的方式来获取。

 

 
  1. //获取JVM的系统属性

  2. import java.util.Properties;

  3. public class ReadJVM {

  4. public static void main(String[] args) {

  5. Properties pps = System.getProperties();

  6. pps.list(System.out);

  7. }

  8. }

 

 

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

1. 使用java.util.Properties类的load()方法示例:

InputStream in = new BufferedInputStream(new FileInputStream(name));Properties p = new Properties();p.load(in);

详细示例一:

 

 
  1. import java.io.FileInputStream;

  2. import java.io.FileNotFoundException;

  3. import java.io.IOException;

  4. import java.util.Enumeration;

  5. import java.util.Properties;

  6. /*

  7.  * 新建一个配置文件(Test.properties),内容可以录入下面的语句。

  8.  * name=JJ

  9.  * Weight=4444

  10.  * Height=3333

  11.  * 

  12.  * 注意:配置文件一定要放到项目的根目录。(此处没有异常处理)

  13.  *

  14.  */

  15. public class getProperties {

  16.     public static void main(String[] args) throws FileNotFoundException, IOException {

  17.         Properties pps = new Properties();

  18.         pps.load(new FileInputStream("Test.properties"));

  19.         Enumeration enum1 = pps.propertyNames();//得到配置文件的名字

  20.         while(enum1.hasMoreElements()) {

  21.             String strKey = (String) enum1.nextElement();

  22.             String strValue = pps.getProperty(strKey);

  23.             System.out.println(strKey + "=" + strValue);

  24.         }

  25.     }

  26. }

详细示例二:

 

 

 
  1. import java.io.BufferedInputStream;

  2. import java.io.FileInputStream;

  3. import java.io.FileOutputStream;

  4. import java.io.IOException;

  5. import java.io.InputStream;

  6. import java.io.OutputStream;

  7. import java.util.Enumeration;

  8. import java.util.Properties;

  9.  
  10. //关于Properties类常用的操作

  11. public class TestProperties {

  12. //根据Key读取Value

  13. public static String GetValueByKey(String filePath, String key) {

  14. Properties pps = new Properties();

  15. try {

  16. InputStream in = new BufferedInputStream (new FileInputStream(filePath));

  17. pps.load(in);

  18. String value = pps.getProperty(key);

  19. System.out.println(key + " = " + value);

  20. return value;

  21.  
  22. }catch (IOException e) {

  23. e.printStackTrace();

  24. return null;

  25. }

  26. }

  27.  
  28. //读取Properties的全部信息

  29. public static void GetAllProperties(String filePath) throws IOException {

  30. Properties pps = new Properties();

  31. InputStream in = new BufferedInputStream(new FileInputStream(filePath));

  32. pps.load(in);

  33. Enumeration en = pps.propertyNames(); //得到配置文件的名字

  34.  
  35. while(en.hasMoreElements()) {

  36. String strKey = (String) en.nextElement();

  37. String strValue = pps.getProperty(strKey);

  38. System.out.println(strKey + "=" + strValue);

  39. }

  40.  
  41. }

  42.  
  43. //写入Properties信息

  44. public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {

  45. Properties pps = new Properties();

  46.  
  47. InputStream in = new FileInputStream(filePath);

  48. //从输入流中读取属性列表(键和元素对)

  49. pps.load(in);

  50. //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。

  51. //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。

  52. OutputStream out = new FileOutputStream(filePath);

  53. pps.setProperty(pKey, pValue);

  54. //以适合使用 load 方法加载到 Properties 表中的格式,

  55. //将此 Properties 表中的属性列表(键和元素对)写入输出流

  56. pps.store(out, "Update " + pKey + " name");

  57. }

  58.  
  59. public static void main(String [] args) throws IOException{<span style="font-family: Arial, Helvetica, sans-serif;"> </span>

  60. <span style="white-space:pre"> </span>WriteProperties("Test.properties","long", "212");

  61. String value = GetValueByKey("Test.properties", "name");

  62. System.out.println(value);

  63. GetAllProperties("Test.properties");

  64. }

  65. }

2.使用class变量的getResourceAsStream()方法
示例:

 

InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
详细示例:

 

 
  1. import java.io.FileNotFoundException;

  2. import java.io.IOException;

  3. import java.util.Enumeration;

  4. import java.util.Properties;

  5. /*

  6. * 新建一个配置文件(Test.properties),内容可以录入下面的语句。

  7. * name=JJ

  8. * Weight=4444

  9. * Height=3333

  10. *

  11. *注意:配置文件一定要放到当前目录下。(目录层次也可以从src下面的文件夹开始但不必包含src,且不必包含反斜杠开头。)

  12. *

  13. */

  14. public class getProperties {

  15. public static void main(String[] args) throws FileNotFoundException, IOException {

  16. Properties pps = new Properties();

  17. pps.load(getProperties.class.getResourceAsStream("Test.properties"));

  18. Enumeration enum1 = pps.propertyNames();//得到配置文件的名字

  19. while(enum1.hasMoreElements()) {

  20. String strKey = (String) enum1.nextElement();

  21. String strValue = pps.getProperty(strKey);

  22. System.out.println(strKey + "=" + strValue);

  23. }

  24. }

  25. }

3.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: 

 

InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
详细示例:

 

 
  1. import java.io.FileNotFoundException;

  2. import java.io.IOException;

  3. import java.util.Enumeration;

  4. import java.util.Properties;

  5. /*

  6. * 新建一个配置文件(Test.properties),内容可以录入下面的语句。

  7. * name=JJ

  8. * Weight=4444

  9. * Height=3333

  10. *

  11. *注意:配置文件一定要放到src目录下。

  12. *

  13. */

  14. public class getProperties {

  15. public static void main(String[] args) throws FileNotFoundException, IOException {

  16. Properties pps = new Properties();

  17. pps.load(getProperties.class.getClassLoader().getResourceAsStream("Test.properties"));

  18. Enumeration enum1 = pps.propertyNames();//得到配置文件的名字

  19. while(enum1.hasMoreElements()) {

  20. String strKey = (String) enum1.nextElement();

  21. String strValue = pps.getProperty(strKey);

  22. System.out.println(strKey + "=" + strValue);

  23. }

  24. }

  25. }

4.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例: 

 

InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);
详细示例:

 

 
  1. import java.io.FileNotFoundException;

  2. import java.io.IOException;

  3. import java.util.Enumeration;

  4. import java.util.Properties;

  5. /*

  6. * 新建一个配置文件(Test.properties),内容可以录入下面的语句。

  7. * name=JJ

  8. * Weight=4444

  9. * Height=3333

  10. *

  11. * 注意:配置文件一定要放到src目录下。

  12. *

  13. */

  14. public class getProperties {

  15. public static void main(String[] args) throws FileNotFoundException, IOException {

  16. Properties pps = new Properties();

  17. pps.load(ClassLoader.getSystemResourceAsStream("Test.properties"));

  18. Enumeration enum1 = pps.propertyNames();//得到配置文件的名字

  19. while(enum1.hasMoreElements()) {

  20. String strKey = (String) enum1.nextElement();

  21. String strValue = pps.getProperty(strKey);

  22. System.out.println(strKey + "=" + strValue);

  23. }

  24. }

  25. }

5.使用java.util.ResourceBundle类的getBundle()方法
示例: 

 

ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
 

这个类提供软件国际化的捷径。通过此类,可以使您所编写的程序可以:

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

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

 

 

  • properties文件的名字是有规范的:一般的命名规范是: 自定义名_语言代码_国别代码.properties
  • 如果是默认的,直接写为:自定义名.properties

比如:
myres_en_US.properties
myres_zh_CN.properties
myres.properties

 

当在中文操作系统下,如果myres_zh_CN.properties、myres.properties两个文件都存在,则优先会使用myres_zh_CN.properties,当myres_zh_CN.properties不存在时候,会使用默认的myres.properties。

没有提供语言和地区的资源文件是系统默认的资源文件。
资源文件都必须是ISO-8859-1编码,因此,对于所有非西方语系的处理,都必须先将之转换为Java Unicode Escape格式。转换方法是通过JDK自带的工具native2ascii.

详细示例:

 

 
  1. import java.util.Locale;

  2. import java.util.ResourceBundle;

  3.  
  4. /**

  5. * 国际化资源绑定测试

  6. *

  7. * @author leizhimin 2009-7-29 21:17:42

  8. */

  9. public class TestResourceBundle {

  10. public static void main(String[] args) {

  11. Locale locale1 = new Locale("zh", "CN");

  12. ResourceBundle resb1 = ResourceBundle.getBundle("myres", locale1);

  13. System.out.println(resb1.getString("aaa"));

  14.  
  15. ResourceBundle resb2 = ResourceBundle.getBundle("myres", Locale.getDefault());

  16. System.out.println(resb1.getString("aaa"));

  17.  
  18. Locale locale3 = new Locale("en", "US");

  19. ResourceBundle resb3 = ResourceBundle.getBundle("myres", locale3);

  20. System.out.println(resb3.getString("aaa"));

  21. }

  22. }

  23. //如果使用默认的Locale,那么在英文操作系统上,会选择myres_en_US.properties或myres.properties资源文件。

 

6.使用java.util.PropertyResourceBundle类的构造函数

示例:

InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);

 

PropertyResourceBundle是ResourceBundle的具体子类,是通过对属性文件的静态字符串管理来语言环境资源。
与其他资源包类型不同,不能为 PropertyResourceBundle 创建子类。相反,要提供含有资源数据的属性文件。ResourceBundle.getBundle 将自动查找合适的属性文件并创建引用该文件的 PropertyResourceBundle
具体用法如下:
创建一个属性文件:conf.properties,内容如下:
BODWEBSERVICEIPADDRESS=D:\\work\\LCEclipse\\workspace\\bodportal\\webapps\\bod\\WEB-INF\\classes\\sysconfig\\policyconfig.xml
在程序中,通过PropertyResourceBundle获取:
private final static String PROPERTIES_NAME = "conf";
public static String getProperties(String configName){
PropertyResourceBundle prbConfig =
(PropertyResourceBundle)PropertyResourceBundle.getBundle(PROPERTIES_NAME);
return prbConfig.getString(configName);
}

四、认识Locale

Locale 对象表示了特定的地理、政治和文化地区。需要 Locale 来执行其任务的操作称为语言环境敏感的 操作,它使用 Locale 为用户量身定制信息。例如,显示一个数值就是语言环境敏感的操作,应该根据用户的国家、地区或文化的风俗/传统来格式化该数值。

使用此类中的构造方法来创建 Locale:
Locale(String language)
Locale(String language, String country)
Locale(String language, String country, String variant)

创建完 Locale 后,就可以查询有关其自身的信息。使用 getCountry 可获取 ISO 国家代码,使用 getLanguage 则获取 ISO 语言代码。可用使用 getDisplayCountry 来获取适合向用户显示的国家名。同样,可用使用 getDisplayLanguage 来获取适合向用户显示的语言名。有趣的是,getDisplayXXX 方法本身是语言环境敏感的,它有两个版本:一个使用默认的语言环境作为参数,另一个则使用指定的语言环境作为参数。

五、中文资源文件的转码 native2ascii

这个工具用法如下:

如果觉得麻烦,可以直接将中文粘贴到里面,回车就可以看到转码后的结果了。

看明白这个了,就不在为struts等web框架的国际化而感到稀奇了。

六、示例

 

 
  1. import java.io.BufferedInputStream;

  2. import java.io.FileInputStream;

  3. import java.io.FileOutputStream;

  4. import java.io.IOException;

  5. import java.io.InputStream;

  6. import java.io.OutputStream;

  7. import java.util.Enumeration;

  8. import java.util.Properties;

  9.  
  10. public class TestMain {

  11.  
  12. // 根据key读取value

  13. public static String readValue(String filePath, String key) {

  14. Properties props = new Properties();

  15. try {

  16. InputStream in = new BufferedInputStream(new FileInputStream(filePath));

  17. props.load(in);

  18. String value = props.getProperty(key);

  19. System.out.println(key + value);

  20. return value;

  21. } catch (Exception e) {

  22. e.printStackTrace();

  23. return null;

  24. }

  25. }

  26.  
  27. // 读取properties的全部信息

  28. public static void readProperties(String filePath) {

  29. Properties props = new Properties();

  30. try {

  31. InputStream in = new BufferedInputStream(new FileInputStream(filePath));

  32. props.load(in);

  33. Enumeration en = props.propertyNames();

  34. while (en.hasMoreElements()) {

  35. String key = (String) en.nextElement();

  36. String Property = props.getProperty(key);

  37. System.out.println(key + Property);

  38. }

  39. } catch (Exception e) {

  40. e.printStackTrace();

  41. }

  42. }

  43.  
  44. // 写入properties信息

  45. public static void writeProperties(String filePath, String parameterName, String parameterValue) {

  46. Properties prop = new Properties();

  47. try {

  48. InputStream fis = new FileInputStream(filePath);

  49. // 从输入流中读取属性列表(键和元素对)

  50. prop.load(fis);

  51. // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。

  52. // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。

  53. OutputStream fos = new FileOutputStream(filePath);

  54. prop.setProperty(parameterName, parameterValue);

  55. // 以适合使用 load 方法加载到 Properties 表中的格式,

  56. // 将此 Properties 表中的属性列表(键和元素对)写入输出流

  57. prop.store(fos, "Update '" + parameterName + "' value");

  58. } catch (IOException e) {

  59. System.err.println("Visit " + filePath + " for updating " + parameterName + " value error");

  60. }

  61. }

  62.  
  63. public static void main(String[] args) {

  64. readValue("info.properties", "url");

  65. writeProperties("info.properties", "age", "21");

  66. readProperties("info.properties");

  67. System.out.println("OK");

  68. }

 

https://blog.csdn.net/fanxiaobin577328725/article/details/52071310

  • 5
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值