zoukankan      html  css  js  c++  java
  • 提供一些常用的属性文件相关的方法

    package com.opslab.util;


    import org.apache.log4j.Logger;

    import java.io.*;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;

    /**
    * 提供一些常用的属性文件相关的方法
    */
    public final class PropertiesUtil {
    public static Logger logger = Logger.getLogger(PropertiesUtil.class);

    /**
    * 从系统属性文件中获取相应的值
    *
    * @param key key
    * @return 返回value
    */
    public final static String key(String key) {
    return System.getProperty(key);
    }

    /**
    * 根据Key读取Value
    *
    * @param filePath 属性文件
    * @param key 需要读取的属性
    */
    public final static String GetValueByKey(String filePath, String key) {
    Properties pps = new Properties();
    try (InputStream in = new BufferedInputStream(new FileInputStream(filePath))) {
    pps.load(in);
    return pps.getProperty(key);
    } catch (IOException e) {
    e.printStackTrace();
    return null;
    }
    }

    public final static Map<String,String> properties(InputStream in){
    Map<String,String> map = new HashMap<>();
    Properties pps = new Properties();
    try {
    pps.load(in);
    } catch (IOException e) {
    logger.error("load properties error:"+e.getMessage());
    }
    Enumeration en = pps.propertyNames();
    while (en.hasMoreElements()) {
    String strKey = (String) en.nextElement();
    String strValue = pps.getProperty(strKey);
    map.put(strKey,strValue);
    }
    return map;
    }
    /**
    * 读取Properties的全部信息
    *
    * @param filePath 读取的属性文件
    * @return 返回所有的属性 key:value<>key:value
    */
    public final static Map<String,String> GetAllProperties(String filePath) throws IOException {
    Map<String,String> map = new HashMap<>();
    Properties pps = new Properties();
    try (InputStream in = new BufferedInputStream(new FileInputStream(filePath))) {
    return properties(in);
    }catch (IOException e){
    logger.error("load properties error");
    }
    return map;
    }

    /**
    * 写入Properties信息
    *
    * @param filePath 写入的属性文件
    * @param pKey 属性名称
    * @param pValue 属性值
    */
    public final static void WriteProperties(String filePath, String pKey, String pValue) throws IOException {
    Properties props = new Properties();

    props.load(new FileInputStream(filePath));
    // 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。
    // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
    OutputStream fos = new FileOutputStream(filePath);
    props.setProperty(pKey, pValue);
    // 以适合使用 load 方法加载到 Properties 表中的格式,
    // 将此 Properties 表中的属性列表(键和元素对)写入输出流
    props.store(fos, "Update '" + pKey + "' value");

    }

    }

  • 相关阅读:
    Codeforces Beta Round #92 (Div. 2 Only) B. Permutations 模拟
    POJ 3281 Dining 最大流 Dinic算法
    POJ 2441 Arrange the BUlls 状压DP
    URAL 1152 Faise Mirrors 状压DP 简单题
    URAL 1039 Anniversary Party 树形DP 水题
    URAL 1018 Binary Apple Tree 树形DP 好题 经典
    pytorch中的forward前向传播机制
    .data()与.detach()的区别
    Argparse模块
    pytorch代码调试工具
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10254862.html
Copyright © 2011-2022 走看看