Properties文件操作

本文介绍了一个用于读取和管理配置文件的实用工具类,包括获取项目根目录、加载配置文件、读取配置项等功能,并提供了初始化配置、保存配置等方法。
private static Logger logger = Logger.getLogger(SysPropUtil.class);

    private static Properties p = new Properties();

    //相对路径
    private static String path;//="\\autoUpdateConfig.properties";

    /**
     * 获取项目更目录(最后没有\)
     * E:\Workspaces\MyEclipseWS\AutoUpdateTool
     * @return
     */
    public static String getConfigPath(){
        String configPath=SysPropUtil.getValue("config.path");
        if (StringUtil.isEmpty(configPath)) {
            configPath=getProjectPath();
        }
        return configPath;
    }
    public static String getProjectPath(){
        String projectPath=System.getProperty("user.dir");
        if (StringUtil.isEmpty(projectPath)) {
            projectPath= new File("").getAbsolutePath();
        }
        return projectPath;
    }
    public static String getClassesPath(){
        return SysPropUtil.class.getClassLoader().getResource("").getPath();
    }

    public static byte[] getRSAPublicKey()throws Exception{ 
        String publicKey=SysPropUtil.getValue("update.key");
        if (publicKey==null) {
            publicKey=UpdateService.getPublicKey();
        }
        return Base64.decodeBase64(publicKey);
    }

    public static void init(String configpath)throws Exception{
        System.out.println("SysPropUtil init("+configpath+") ");
        logger.debug("SysPropUtil init("+configpath+") ");
        loadConfig(p,configpath);
    }


    private static void loadConfig(Properties prop,String configpath) throws Exception{

        FileInputStream fis = null;
        String configFile = new File("").getAbsolutePath()+File.separator+ configpath;
        path=configFile;
        logger.debug("loadConfig path : "+configFile);
        System.out.println("loadConfig path : "+configFile);
        try {
            File file=new File(configFile);
            if (!file.exists()) {
                file.createNewFile();
            }
            fis = new FileInputStream(file);
            prop.load(fis);  
        } catch(Exception e) {
            e.printStackTrace();
            System.out.println("load "+configpath+" error:" + e.getMessage());
            throw e;
        } finally{
            if(fis!=null){
                try {fis.close();} catch(Exception e) {}                
            }
        }


    }

    public static String getValue(String key) {
        if (p != null) {

            try {
                return new String (p.getProperty(key).getBytes(),"utf-8");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
                logger.error("SysPropUtil getValue key="+key+" exception : "+e);
                return null;
            }
        } else {
            return null;
        }
    }



    public static void setValue(String key,String value) {
        if (p != null) {
            p.setProperty(key, value);
        }
    }

    public static void saveValue(String key,String value) {
        try {
            OutputStream out = new FileOutputStream(path);
            p.setProperty(key, value);
            p.store(out, "update");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值