• 类 Properties 使用 转


    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   
  • 相关阅读:
    微服务架构总结
    微服务-网关服务
    HttpClient-RestTemplate-Feign
    RPC和REST
    Springmvc的拦截器执行顺序及各方法作用
    秒杀系统优化方案(下)吐血整理
    秒杀系统优化方案(上)吐血整理
    分布式session的管理
    缓存设计——缓存和数据库的数据一致性
    个人理解的javascript作用域链与闭包
  • 原文地址:https://www.cnblogs.com/gym333/p/2664075.html
Copyright © 2020-2023  润新知