它也是一种key-value结构的集合,它是HashTable的子类,它同HashTable在某些规则上是一样,也是添加了synchronized关键字的。例外,同样,不允许放置null 值 和null键
    它的主要使用场景:从配置文件中 加载内容到程序来中

    mysql.properties:

    1. jdbc.url=1231wrwer
    2. jdbc.userName=userpu
    3. jdbc.password=123456
    4. jdbc.driverClassName=234

    key为等号左边的部分,value为等号右边的值

    1. import java.io.File;
    2. import java.io.FileInputStream;
    3. import java.io.IOException;
    4. import java.util.Properties;
    5. import com.woniuxy.java21.bean.CarBean;
    6. import com.woniuxy.java21.bean.StudentBean;
    7. public class PropertiesStudy {
    8. public static void main(String[] args) {
    9. // TODO Auto-generated method stub
    10. // study01();
    11. study02();
    12. }
    13. private static void study02() {
    14. // TODO Auto-generated method stub
    15. Properties props = new Properties();
    16. FileInputStream fis = null;
    17. try {
    18. // File.separatorChar 代表 \\ 在windows系统中
    19. String path = System.getProperty("user.dir") + File.separatorChar + "src" + File.separatorChar +"mysql.properties" ;
    20. // System.out.println(path);
    21. File file = new File(path);
    22. fis = new FileInputStream(file);
    23. props.load(fis);
    24. System.out.println(props.get("jdbc.url"));
    25. System.out.println(props.get("jdbc.userName"));
    26. System.out.println(props.get("jdbc.password"));
    27. System.out.println(props.get("jdbc.driverClassName"));
    28. } catch (Exception e) {
    29. // TODO: handle exception
    30. e.printStackTrace();
    31. }finally {
    32. try {
    33. fis.close();
    34. } catch (IOException e) {
    35. // TODO Auto-generated catch block
    36. e.printStackTrace();
    37. }
    38. }
    39. }
    40. private static void study01() {
    41. // TODO Auto-generated method stub
    42. // Properties props = new Properties();
    43. // //同样可以放置null值和null键
    44. // props.put(null, null);
    45. // //也可以放置其他东西
    46. // props.put(new CarBean(), new StudentBean());
    47. // props.put("userName", "小张");
    48. // System.out.println(props.get("userName"));
    49. }
    50. }