JAVA操作配置文件properties

一、Java Properties类

    Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。像Python支持的配置文件是.ini文件,同样,它也有自己读取配置文件的类ConfigParse,方便程序员或用户通过该类的方法来修改.ini配置文件。在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释。

Properties类继承自Hashtable,如下:

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

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 (),清除所有装载的 键 - 值对。该方法在基类中提供。

 

二、Java读取Properties文件

    Java读取Properties文件的方法有很多,详见: Java读取Properties文件的六种方法

但是最常用的还是通过java.lang.Class类的getResourceAsStream(String name)方法来实现,如下可以这样调用:

InputStream in = getClass().getResourceAsStream("资源Name");作为我们写程序的,用此一种足够。

或者下面这种也常用:

InputStream in = new BufferedInputStream(new FileInputStream(filepath));

---------------------------------------------------------分割线-----------------------------------------------

一、读取配置文件的六种方法

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


二、对应代码

  1. package com.test.modul.utils;
  2. import java.io.BufferedInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.util.Locale;
  9. import java.util.Properties;
  10. import java.util.PropertyResourceBundle;
  11. import java.util.ResourceBundle;
  12. /**
  13. * 加载properties文件的方式
  14. *
  15. * @author wjzuo
  16. *
  17. */
  18. public class LoadPropertiesFileUtil {
  19. private static String basePath = "src/main/java/com/test/modul/utils/prop.properties";
  20. private static String path = "";
  21. /**
  22. * 一、 使用java.util.Properties类的load(InputStream in)方法加载properties文件
  23. *
  24. * @return
  25. */
  26. public static String getPath1() {
  27. try {
  28. InputStream in = new BufferedInputStream( new FileInputStream(
  29. new File(basePath)));
  30. Properties prop = new Properties();
  31. prop.load(in);
  32. path = prop.getProperty( "path");
  33. } catch (FileNotFoundException e) {
  34. System.out.println( "properties文件路径书写有误,请检查!");
  35. e.printStackTrace();
  36. } catch (IOException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. }
  40. return path;
  41. }
  42. /**
  43. * 二、 使用java.util.ResourceBundle类的getBundle()方法
  44. * 注意:这个getBundle()方法的参数只能写成包路径+properties文件名,否则将抛异常
  45. *
  46. * @return
  47. */
  48. public static String getPath2() {
  49. ResourceBundle rb = ResourceBundle
  50. .getBundle( "com/test/modul/utils/prop");
  51. path = rb.getString( "path");
  52. return path;
  53. }
  54. /**
  55. * 三、 使用java.util.PropertyResourceBundle类的构造函数
  56. *
  57. * @return
  58. */
  59. public static String getPath3() {
  60. InputStream in;
  61. try {
  62. in = new BufferedInputStream( new FileInputStream(basePath));
  63. ResourceBundle rb = new PropertyResourceBundle(in);
  64. path = rb.getString( "path");
  65. } catch (FileNotFoundException e) {
  66. // TODO Auto-generated catch block
  67. e.printStackTrace();
  68. } catch (IOException e) {
  69. // TODO Auto-generated catch block
  70. e.printStackTrace();
  71. }
  72. return path;
  73. }
  74. /**
  75. * 四、 使用class变量的getResourceAsStream()方法
  76. * 注意:getResourceAsStream()方法的参数按格式写到包路径+properties文件名+.后缀
  77. *
  78. * @return
  79. */
  80. public static String getPath4() {
  81. InputStream in = LoadPropertiesFileUtil.class
  82. .getResourceAsStream( "/com/test/modul/utils/prop.properties");
  83. Properties p = new Properties();
  84. try {
  85. p.load(in);
  86. path = p.getProperty( "path");
  87. } catch (IOException e) {
  88. // TODO Auto-generated catch block
  89. e.printStackTrace();
  90. }
  91. return path;
  92. }
  93. /**
  94. * 五、
  95. * 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
  96. * getResourceAsStream(name)方法的参数必须是包路径+文件名+.后缀
  97. * 否则会报空指针异常
  98. * @return
  99. */
  100. public static String getPath5() {
  101. InputStream in = LoadPropertiesFileUtil.class.getClassLoader()
  102. .getResourceAsStream( "com/test/modul/utils/prop.properties");
  103. Properties p = new Properties();
  104. try {
  105. p.load(in);
  106. path = p.getProperty( "path");
  107. } catch (IOException e) {
  108. // TODO Auto-generated catch block
  109. e.printStackTrace();
  110. }
  111. return path;
  112. }
  113. /**
  114. * 六、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
  115. * getSystemResourceAsStream()方法的参数格式也是有固定要求的
  116. *
  117. * @return
  118. */
  119. public static String getPath6() {
  120. InputStream in = ClassLoader
  121. .getSystemResourceAsStream( "com/test/modul/utils/prop.properties");
  122. Properties p = new Properties();
  123. try {
  124. p.load(in);
  125. path = p.getProperty( "path");
  126. } catch (IOException e) {
  127. // TODO Auto-generated catch block
  128. e.printStackTrace();
  129. }
  130. return path;
  131. }
  132. public static void main(String[] args) {
  133. System.out.println(LoadPropertiesFileUtil.getPath1());
  134. System.out.println(LoadPropertiesFileUtil.getPath2());
  135. System.out.println(LoadPropertiesFileUtil.getPath3());
  136. System.out.println(LoadPropertiesFileUtil.getPath4());
  137. System.out.println(LoadPropertiesFileUtil.getPath5());
  138. System.out.println(LoadPropertiesFileUtil.getPath6());
  139. }
  140. }

其中第一、四、五、六种方式都是先获得文件的输入流,然后通过Properties类的load(InputStream inStream)方法加载到Properties对象中,最后通过Properties对象来操作文件内容;

第二、三中方式是通过ResourceBundle类来加载Properties文件,然后ResourceBundle对象来操做properties文件内容。

其中最重要的就是每种方式加载文件时,文件的路径需要按照方法的定义的格式来加载,否则会抛出各种异常,比如空指针异常。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值