Java知识【Properties集合】

曾经终败给现在 2023-10-02 21:51 79阅读 0赞

目录

4.Properties集合

4.1Properties作为Map集合的使用【应用】

4.2Properties作为Map集合的特有方法【应用】

4.3Properties和IO流相结合的方法【应用】

4.4Properties集合练习【应用】


4.Properties集合

4.1Properties作为Map集合的使用【应用】

  • Properties介绍

    • 是一个Map体系的集合类
    • Properties可以保存到流中或从流中加载
    • 属性列表中的每个键及其对应的值都是一个字符串
  • Properties基本使用

    1. public class PropertiesDemo01 {
    2. public static void main(String[] args) {
    3. //创建集合对象
    4. // Properties<String,String> prop = new Properties<String,String>(); //错误
    5. Properties prop = new Properties();
    6. //存储元素
    7. prop.put("itheima001", "佟丽娅");
    8. prop.put("itheima002", "赵丽颖");
    9. prop.put("itheima003", "刘诗诗");
    10. //遍历集合
    11. Set<Object> keySet = prop.keySet();
    12. for (Object key : keySet) {
    13. Object value = prop.get(key);
    14. System.out.println(key + "," + value);
    15. }
    16. }
    17. }

4.2Properties作为Map集合的特有方法【应用】

  • 特有方法






















    方法名 说明
    Object setProperty(String key, String value) 设置集合的键和值,都是String类型,底层调用 Hashtable方法 put
    String getProperty(String key) 使用此属性列表中指定的键搜索属性
    Set<String> stringPropertyNames() 从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
  • 示例代码

    1. public class PropertiesDemo02 {
    2. public static void main(String[] args) {
    3. //创建集合对象
    4. Properties prop = new Properties();
    5. //Object setProperty(String key, String value):设置集合的键和值,都是String类型
    6. prop.setProperty("itheima001", "佟丽娅");
    7. prop.setProperty("itheima002", "赵丽颖");
    8. prop.setProperty("itheima003", "刘诗诗");
    9. //String getProperty(String key):使用此属性列表中指定的键搜索属性
    10. // System.out.println(prop.getProperty("itheima001"));
    11. // System.out.println(prop.getProperty("itheima0011"));
    12. // System.out.println(prop);
    13. //Set<String> stringPropertyNames():从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
    14. Set<String> names = prop.stringPropertyNames();
    15. for (String key : names) {
    16. // System.out.println(key);
    17. String value = prop.getProperty(key);
    18. System.out.println(key + "," + value);
    19. }
    20. }
    21. }

4.3Properties和IO流相结合的方法【应用】

  • 和IO流结合的方法


















    方法名 说明
    void load(Reader reader) 从输入字符流读取属性列表(键和元素对)
    void store(Writer writer, String comments) 将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式写入输出字符流
  • 示例代码

    1. public class PropertiesDemo03 {
    2. public static void main(String[] args) throws IOException {
    3. //把集合中的数据保存到文件
    4. // myStore();
    5. //把文件中的数据加载到集合
    6. myLoad();
    7. }
    8. private static void myLoad() throws IOException {
    9. Properties prop = new Properties();
    10. //void load(Reader reader):
    11. FileReader fr = new FileReader("myOtherStream\\fw.txt");
    12. prop.load(fr);
    13. fr.close();
    14. System.out.println(prop);
    15. }
    16. private static void myStore() throws IOException {
    17. Properties prop = new Properties();
    18. prop.setProperty("itheima001","佟丽娅");
    19. prop.setProperty("itheima002","赵丽颖");
    20. prop.setProperty("itheima003","刘诗诗");
    21. //void store(Writer writer, String comments):
    22. FileWriter fw = new FileWriter("myOtherStream\\fw.txt");
    23. prop.store(fw,null);
    24. fw.close();
    25. }
    26. }

4.4Properties集合练习【应用】

  • 案例需求

    在Properties文件中手动写上姓名和年龄,读取到集合中,将该数据封装成学生对象,写到本地文件

  • 实现步骤

    • 创建Properties集合,将本地文件中的数据加载到集合中
    • 获取集合中的键值对数据,封装到学生对象中
    • 创建序列化流对象,将学生对象序列化到本地文件中
  • 代码实现

    学生类

    1. public class Student implements Serializable {
    2. private static final long serialVersionUID = 1L;
    3. private String name;
    4. private int age;
    5. public Student() {
    6. }
    7. public Student(String name, int age) {
    8. this.name = name;
    9. this.age = age;
    10. }
    11. public String getName() {
    12. return name;
    13. }
    14. public void setName(String name) {
    15. this.name = name;
    16. }
    17. public int getAge() {
    18. return age;
    19. }
    20. public void setAge(int age) {
    21. this.age = age;
    22. }
    23. @Override
    24. public String toString() {
    25. return "Student{" +
    26. "name='" + name + '\'' +
    27. ", age=" + age +
    28. '}';
    29. }
    30. }

    测试类

    1. public class Test {
    2. public static void main(String[] args) throws IOException {
    3. //1.创建Properties集合,将本地文件中的数据加载到集合中
    4. Properties prop = new Properties();
    5. FileReader fr = new FileReader("prop.properties");
    6. prop.load(fr);
    7. fr.close();
    8. //2.获取集合中的键值对数据,封装到学生对象中
    9. String name = prop.getProperty("name");
    10. int age = Integer.parseInt(prop.getProperty("age"));
    11. Student s = new Student(name,age);
    12. //3.创建序列化流对象,将学生对象序列化到本地文件中
    13. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
    14. oos.writeObject(s);
    15. oos.close();
    16. }
    17. }

发表评论

表情:
评论列表 (有 0 条评论,79人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java集合知识总结

    今天在看了好多个集合帖子后发现讲的都是一半一半的,这下就逼死我这个强迫症患者了.于是自己写点,有不足之处还望各位道友多多指点 一、Collection接口 1.Col

    相关 Java集合部分知识

    集合中的接口与类之间的关系如下图所示: ![70][] 其中蓝色标注的为接口,红色标注的为类。 具体如下: 1.Set接口表示的集合不能包含重复的元素; 2.Hash