新浪微博开放的源码中对 读写properties文件的封装

主要是用到了静态块,和反射机制得到类的路径。 


package com.weibo.weibo4j.util;

 

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Properties;

 

@SuppressWarnings("static-access")

public class WeiboConfig

{

    private static String filePath = WeiboConfig.class.getResource("/").getPath() + "sinaywconfig.properties";

 

    public WeiboConfig()

    {

    }

 

    private static Properties props = new Properties();

    static

    {

        try

        {

            String filePaths = new URLEncodeUtils().decodeURL(filePath);

            System.out.println(filePaths);

            props.load(new FileInputStream(filePaths));

        }

        catch (FileNotFoundException e)

        {

            e.printStackTrace();

        }

        catch (IOException e)

        {

            e.printStackTrace();

        }

    }

 

    public static String getValue(String key)

    {

        return props.getProperty(key);

    }

 

    public static void updateProperties(String key, String value)

    {

        props.setProperty(key, value);

    }

}

 

你可能感兴趣的:(读写properties文件)