类 Properties 的使用 实例

java 代码
  1. DataBase.properties的内容   
  2.   
  3.   
  4. driver=com.mysql.jdbc.Driver   
  5. url=jdbc:mysql://127.0.0.1:3306/jpetstore   
  6. username=root   
  7. password=dongguoh   
  8.   
  9. DataBase.xml的内容   
  10.   
  11. <?xml version="1.0" encoding="UTF-8"?>   
  12. <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">   
  13. <properties>   
  14. <comment>PropertiesTest</comment>   
  15. <entry key="driver">com.mysql.jdbc.Driver</entry>   
  16. <entry key="url">jdbc:mysql://127.0.0.1:3306/jpetstore</entry>   
  17. <entry key="username">root</entry>   
  18. <entry key="password">dongguoh</entry>   
  19. </properties>   
  20.   
  21. 下面是测试类   
  22.   
  23. package DataBase;   
  24. import java.io.FileInputStream;   
  25. import java.io.FileNotFoundException;   
  26. import java.io.IOException;   
  27. import java.util.Iterator;   
  28. import java.util.Properties;   
  29. import java.util.Set;   
  30.   
  31.   
  32. public class TestProperties {   
  33.   
  34. public static void main(String[] args) {   
  35.     TestProperties test=new TestProperties();   
  36.     test.luanch();   
  37.     test.luanchXML();   
  38.   
  39. }   
  40. private void luanch(){   
  41.     Properties ppt=new Properties();   
  42.     try {   
  43.      String path=this.getClass().getResource("/").getPath();   
  44.      path=path+"DataBase/DataBase.properties";   
  45.      FileInputStream fis=new FileInputStream(path);   
  46.      ppt.load(fis);   
  47.      fis.close();   
  48.      Set set=ppt.keySet();    
  49.      Iterator it=set.iterator();    
  50. System.out.println("*********显示的读取DataBase.properties显示的内容*****");   
  51.      while(it.hasNext()){   
  52.       String id=(String)it.next();   
  53. System.out.println(id+"="+ppt.getProperty(id));   
  54.      }   
  55.       
  56. System.out.println("另外一种显示方式");     
  57.      ppt.list(System.out);   
  58.     } catch (FileNotFoundException e) {   
  59.      System.out.println("找不到DataBase.properties这个文件,或者是路径发生错误");   
  60.     } catch (IOException e) {   
  61.      System.out.println("加载DataBase.properties文件时出错!!");   
  62.     }    
  63. }   
  64. private void luanchXML(){   
  65.     Properties ppt=new Properties();   
  66.     try {   
  67.      String path=this.getClass().getResource("/").getPath();   
  68.      path=path+"DataBase/DataBase.xml";   
  69. System.out.println(path);   
  70.      FileInputStream fis=new FileInputStream(path);   
  71.      ppt.loadFromXML(fis);   
  72.      fis.close();   
  73.      Set set=ppt.keySet();    
  74.      Iterator it=set.iterator();   
  75. System.out.println("*********显示的读取DataBase.xml 显示的内容*****");   
  76.      while(it.hasNext()){   
  77.       String id=(String)it.next();   
  78. System.out.println(id+"="+ppt.getProperty(id));   
  79.      }   
  80.     } catch (FileNotFoundException e) {   
  81.      System.out.println("找不到DataBase.xml 这个文件,或者是路径发生错误");   
  82.     } catch (IOException e) {   
  83.      System.out.println("加载DataBase.xml 文件时出错!!");   
  84.     }    
  85. }   
  86.   
  87. }   
  88.   
  89.   
  90.   
  91. 结果:   
  92.   
  93. *********显示的读取DataBase.properties显示的内容*****   
  94. password=dongguoh   
  95. url=jdbc:mysql://127.0.0.1:3306/jpetstore   
  96. driver=com.mysql.jdbc.Driver   
  97. username=root   
  98. 另外一种显示方式   
  99. -- listing properties --   
  100. url=jdbc:mysql://127.0.0.1:3306/jpetstore   
  101. password=dongguoh   
  102. driver=com.mysql.jdbc.Driver   
  103. username=root   
  104. /E:/MyJavaProject/Ibatis/WebRoot/WEB-INF/classes/DataBase/DataBase.xml   
  105. *********显示的读取DataBase.xml 显示的内容*****   
  106. password=dongguoh   
  107. url=jdbc:mysql://127.0.0.1:3306/jpetstore   
  108. driver=com.mysql.jdbc.Driver   
  109. username=root   
  110.   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值