[转帖]Java Properties 类读取配置文件信息_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3167 | 回复: 0   主题: [转帖]Java Properties 类读取配置文件信息        下一篇 
赖文婷
注册用户
等级:少校
经验:1094
发帖:81
精华:0
注册:2012-11-5
状态:离线
发送短消息息给赖文婷 加好友    发送短消息息给赖文婷 发消息
发表于: IP:您无权察看 2012-11-12 15:46:59 | [全部帖] [楼主帖] 楼主

Java Properties 类读取配置文件信息在平时写程序的时候,有些参数是经常改变的,而这种改变不是我们预知的。java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties文件中可以用"#"来作注释,properties文件在Java编程中用到的地方很多,操作很方便。

1、获取JVM的系统属性

1.                  import java.util.Properties;

2.                   

3.                  class PropTest {

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

5.                      Properties pps = System.getProperties();

6.                      pps.list(System.out);

7.                      }

8.                  }

2、读取配置文件

配置文件config.ini

1.                  author=ZJ

2.                  user=all

3.                  copyright=2006-2007

获取配置文件代码

1.                  import java.io.FileInputStream;

2.                  import java.util.Enumeration;

3.                  import java.util.Properties;

4.                   

5.                  class PropTest {

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

7.                  Properties pps=new Properties();

8.                      try {

9.                          pps.load(new FileInputStream("config.ini"));

10. Enumeration enum1 = pps.propertyNames();
11. while (enum1.hasMoreElements()) {
      12. String strKey = (String) enum1.nextElement();
      13. String strValue = pps.getProperty(strKey);
      14. System.out.println(strKey + "=" + strValue);
15. }
16. } catch (Exception e) {
      17. e.printStackTrace();
18. }
19. }
20. }


3、另一个操作properties文件的实例

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.
            16. try {
                  17. InputStream in = new BufferedInputStream (new FileInputStream(filePath));
                  18. props.load(in);
                  19. String value = props.getProperty (key);
                  20. System.out.println(key+value);
                  21. return value;
            22. } catch (Exception e) {
                  23. e.printStackTrace();
                  24. return null;
            25. }
      26. }
      27.
      28. //读取properties的全部信息
      29. public static void readProperties(String filePath) {
            30. Properties props = new Properties();
            31.
            32. try {
                  33. InputStream in = new BufferedInputStream (new FileInputStream(filePath));
                  34. props.load(in);
                  35. Enumeration en = props.propertyNames();
                  36. while (en.hasMoreElements()) {
                        37. String key = (String) en.nextElement();
                        38. String Property = props.getProperty (key);
                        39. System.out.println(key+Property);
                  40. }
            41. } catch (Exception e) {
                  42. e.printStackTrace();
            43. }
      44. }
      45.
      46. //写入properties信息
      47. public static void writeProperties(String filePath,String parameterName,String parameterValue) {
            48. Properties prop = new Properties();
            49.
            50. try {
                  51. InputStream fis = new FileInputStream(filePath);
                  52. //从输入流中读取属性列表(键和元素对)
                  53. prop.load(fis);
                  54. //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性,强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
                  55. OutputStream fos = new FileOutputStream(filePath);
                  56. prop.setProperty(parameterName, parameterValue);
                  57. //以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流
                  58. prop.store(fos, "Update '" + parameterName + "' value");
            59. } catch (IOException e) {
                  60. System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
            61. }
      62. }
      63.
      64. public static void main(String[] args) {
            65. readValue("info.properties","url");
            66. writeProperties("info.properties","age","21");
            67. readProperties("info.properties" );
            68. System.out.println("OK");
      69. }
70. }




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论