yxhxj2006

常用链接

统计

最新评论

JAVA读取Properties配置文件


JAVA获取文件绝对路径Thread.currentThread().getContextClassLoader().getResource("abc.properties");                                   


 配置文件:XXX.properties 常用操作


 1:加载properties配置文件
  /**
     * 根据key从Properties对象中得到相应的值
     * 
@param key
     * 
@return 对应的值
     
*/
    public static String getValue(String key) {
        if (p !=  null) {
            return p.getProperty(key);
        }
        String property = null
        InputStream in = null;
        
        try {
            System.out.println("gleepay.properties is path : " + gleepayURL.getFile());
            File file = new File(gleepayURL.getFile());
            in = new FileInputStream(file);
            p = new Properties();
            p.load(in);
            property = p.getProperty(key);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }  finally {
            if (in != null) {
                try {
                        in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return property;
    }

2:写入propertis配置信息
/**
     * 写入properties信息
     * 
@param parameterKey
     * 
@param parameterValue
     * 
     * 
@return
     
*/
    public static void writeProperties_MACkey(String parameterKey, String parameterValue) {
        String filePath = MACkeyURL.getFile();
        Properties prop = new Properties();
        try {
            InputStream fis = new FileInputStream(filePath);
            // 从输入流中读取属性列表(键和元素对)
            prop.load(fis);
            // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
            
// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
            OutputStream fos = new FileOutputStream(filePath);
            prop.setProperty(parameterKey, parameterValue);
            // 以适合使用 load 方法加载到 Properties 表中的格式,
            
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
            prop.store(fos, "Update '" + parameterKey + "' value");
        } catch (IOException e) {
            System.err.println("Visit " + filePath + " for updating "
                    + parameterKey + " value error");
            e.printStackTrace();
        }
    }
    

3:根据key读取propertis文件value值
    /**
     * 根据key读取value
     * 
@param filePath
     * 
@param key
     * 
@return 对应的值
     
*/
    public static String readValueByKey(String filePath, String key) {
        System.out.println("properties file is path : " + filePath);
        Properties props = new Properties();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);
            String value = props.getProperty(key);
            System.out.println(" --> " + key + "=" + value);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
4:读取properties配置文件所有信息
    /**
     *  读取properties的全部信息
     * 
@param filePath
     * 
@return
     
*/
    @SuppressWarnings("unchecked")
    public static void readPropertiesAll(String filePath) {
        System.out.println("properties file is path : " + filePath);
        Properties props = new Properties();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);
            Enumeration en = props.propertyNames();
            while (en.hasMoreElements()) {
                String key = (String) en.nextElement();
                String Property = props.getProperty(key);
                System.out.println(" --> " + key + "=" + Property);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

posted on 2013-08-08 23:43 奋斗成就男人 阅读(155) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: