IO流 p11 Properties

打印 上一主题 下一主题

主题 916|帖子 916|积分 2748

# Properties类

  • 基本介绍


    • 专门用于读写配置文件的集合类
      配置文件的格式:
      键=值
      键=值
    • 注意:键值对不需要有空格,值不需要用引号一起来。默认类型是String。
    • Properties的常见方法

      • load:加载配置文件的键值对 到Properties对象;
      • list:将数据显示到指定设备/流对象;
      • getProperty(key):根据键获取值;
      • setProperty(key, value):设置键值对到Properties对象;
      • store:将Properties中的键值对存储到配置文件,在idea中,保存信息到配置文件,如果含有中文,会存储为unicode码;


  • 读文件
    示例文件:mysql.properties
    1. ip=192.168.100.100
    2. user=root
    3. pwd=12345
    复制代码
    代码演示:
    1. import java.io.FileNotFoundException;
    2. import java.io.FileReader;
    3. import java.io.IOException;
    4. import java.util.Properties;
    5. /**
    6. * @author: 86199
    7. * @date: 2023/5/8 20:06
    8. * @description:
    9. */
    10. public class Properties02 {
    11.     public static void main(String[] args) throws IOException {
    12.         //使用Properties类 来读取mysql.properties 文件
    13.         //1. 创建Properties对象
    14.         Properties properties = new Properties();
    15.         //2. 加载指定配置文件
    16.         properties.load(new FileReader("src\\mysql.properties"));
    17.         //3. 把 k-v 显示到控制台
    18.         properties.list(System.out);
    19.         //4. 根据key获取对应的值
    20.         String user = properties.getProperty("user");
    21.         String pwd = properties.getProperty("pwd");
    22.         System.out.println("用户名 = " + user);
    23.         System.out.println("密码 = " + pwd);
    24.     }
    25. }
    26. /*
    27. 运行结果:
    28. -- listing properties --
    29. user=root
    30. pwd=12345
    31. ip=192.168.100.100
    32. root
    33. 12345
    34. */
    复制代码
  • 修改文件
    1. import java.io.FileOutputStream;
    2. import java.io.FileWriter;
    3. import java.io.IOException;
    4. import java.util.Properties;
    5. /**
    6. * @author: 86199
    7. * @date: 2023/5/8 20:52
    8. * @description:
    9. */
    10. public class Properties03 {
    11.     public static void main(String[] args) throws IOException {
    12.         //load加载的时候加载到properties对象,是继承了hashtable的,
    13.         // 所以相同key的就替换value了
    14.         //使用Properties类 来创建 配置文件,修改配置文件内容
    15.         Properties properties = new Properties();
    16.         //创建
    17.         //1. 如果该文件没有这个key,就是创建
    18.         //2. 如果该文件有这个key,就是修改
    19.         /*
    20.             Properties  父类是 Hashtable,底层就是Hashtable 核心方法
    21.                 public synchronized V put(K key, V value) {
    22.                     // Make sure the value is not null
    23.                     if (value == null) {
    24.                         throw new NullPointerException();
    25.                     }
    26.                     // Makes sure the key is not already in the hashtable.
    27.                     Entry<?,?> tab[] = table;
    28.                     int hash = key.hashCode();
    29.                     int index = (hash & 0x7FFFFFFF) % tab.length;
    30.                     @SuppressWarnings("unchecked")
    31.                     Entry<K,V> entry = (Entry<K,V>)tab[index];
    32.                     for(; entry != null ; entry = entry.next) {
    33.                         if ((entry.hash == hash) && entry.key.equals(key)) {
    34.                             V old = entry.value;
    35.                             entry.value = value;//如果 key 存在,就替换
    36.                             return old;
    37.                         }
    38.                     }
    39.                     addEntry(hash, key, value, index);//如果是新k,就addEntry
    40.                     return null;
    41.                 }
    42.          */
    43.         properties.setProperty("charset", "utf8");
    44.         properties.setProperty("user","汤姆");//注意中文保存时,是保存中文的 unicode码
    45.         properties.setProperty("pwd","888888");
    46.         //将k-v 存储到文件中即可
    47.         properties.store(new FileOutputStream("src\\mysql2.properties"), null);
    48.         System.out.println("保存配置文件成功~~");
    49.     }
    50. }
    复制代码
    mysql2.properties文件内容:
    1. #Mon May 08 21:16:41 CST 2023
    2. user=\u6C64\u59C6
    3. pwd=888888
    4. charset=utf8
    复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

我可以不吃啊

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表