• Java配置文件Properties的读取、写入与更新操作


    1. /**  
    2. * 实现对Java配置文件Properties的读取、写入与更新操作  
    3. */   
    4. package test;   
    5.   
    6. import java.io.BufferedInputStream;   
    7. import java.io.FileInputStream;   
    8. import java.io.FileNotFoundException;   
    9. import java.io.FileOutputStream;   
    10. import java.io.IOException;   
    11. import java.io.InputStream;   
    12. import java.io.OutputStream;   
    13. import java.util.Properties;   
    14.   
    15.   
    16. /**  
    17. * @author  
    18. * @version  
    19. */   
    20. public class SetSystemProperty {   
    21.     //属性文件的路径   
    22.     static String profilepath="mail.properties";   
    23.     /**  
    24.     * 采用静态方法  
    25.     */   
    26.     private static Properties props = new Properties();   
    27.     static {   
    28.         try {   
    29.             props.load(new FileInputStream(profilepath));   
    30.         } catch (FileNotFoundException e) {   
    31.             e.printStackTrace();   
    32.             System.exit(-1);   
    33.         } catch (IOException e) {          
    34.             System.exit(-1);   
    35.         }   
    36.     }   
    37.   
    38.     /**  
    39.     * 读取属性文件中相应键的值  
    40.     * @param key  
    41.     *            主键  
    42.     * @return String  
    43.     */   
    44.     public static String getKeyValue(String key) {   
    45.         return props.getProperty(key);   
    46.     }   
    47.   
    48.     /**  
    49.     * 根据主键key读取主键的值value  
    50.     * @param filePath 属性文件路径  
    51.     * @param key 键名  
    52.     */   
    53.     public static String readValue(String filePath, String key) {   
    54.         Properties props = new Properties();   
    55.         try {   
    56.             InputStream in = new BufferedInputStream(new FileInputStream(   
    57.                     filePath));   
    58.             props.load(in);   
    59.             String value = props.getProperty(key);   
    60.             System.out.println(key +"键的值是:"+ value);   
    61.             return value;   
    62.         } catch (Exception e) {   
    63.             e.printStackTrace();   
    64.             return null;   
    65.         }   
    66.     }   
    67.       
    68.     /**  
    69.     * 更新(或插入)一对properties信息(主键及其键值)  
    70.     * 如果该主键已经存在,更新该主键的值;  
    71.     * 如果该主键不存在,则插件一对键值。  
    72.     * @param keyname 键名  
    73.     * @param keyvalue 键值  
    74.     */   
    75.     public static void writeProperties(String keyname,String keyvalue) {          
    76.         try {   
    77.             // 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。   
    78.             // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。   
    79.             OutputStream fos = new FileOutputStream(profilepath);   
    80.             props.setProperty(keyname, keyvalue);   
    81.             // 以适合使用 load 方法加载到 Properties 表中的格式,   
    82.             // 将此 Properties 表中的属性列表(键和元素对)写入输出流   
    83.             props.store(fos, "Update '" + keyname + "' value");   
    84.         } catch (IOException e) {   
    85.             System.err.println("属性文件更新错误");   
    86.         }   
    87.     }   
    88.   
    89.     /**  
    90.     * 更新properties文件的键值对  
    91.     * 如果该主键已经存在,更新该主键的值;  
    92.     * 如果该主键不存在,则插件一对键值。  
    93.     * @param keyname 键名  
    94.     * @param keyvalue 键值  
    95.     */   
    96.     public void updateProperties(String keyname,String keyvalue) {   
    97.         try {   
    98.             props.load(new FileInputStream(profilepath));   
    99.             // 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。   
    100.             // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。   
    101.             OutputStream fos = new FileOutputStream(profilepath);              
    102.             props.setProperty(keyname, keyvalue);   
    103.             // 以适合使用 load 方法加载到 Properties 表中的格式,   
    104.             // 将此 Properties 表中的属性列表(键和元素对)写入输出流   
    105.             props.store(fos, "Update '" + keyname + "' value");   
    106.         } catch (IOException e) {   
    107.             System.err.println("属性文件更新错误");   
    108.         }   
    109.     }   
    110.     //测试代码   
    111.     public static void main(String[] args) {   
    112.         readValue("mail.properties""MAIL_SERVER_PASSWORD");   
    113.         writeProperties("MAIL_SERVER_INCOMING""327@qq.com");          
    114.         System.out.println("操作完成");   
    115.     }   
  • 相关阅读:
    深入浅出前端本地储存
    移动端H5开发常用技巧总结
    Web API
    嵌入式软件工程师面试题目整理(二)
    嵌入式软件工程师面试题目整理(一)
    扒一扒ELF文件
    SpringCloud — (1) 服务治理Eureka
    asp.net练习①——Application聊天室
    js数字金额转换为大写金额
    SQL触发器
  • 原文地址:https://www.cnblogs.com/jpfss/p/9790619.html
Copyright © 2020-2023  润新知