PropertiesUtil

自己封装了一个PropertiesUtil,只做了一部分事情,主要是想解决配置文件读取的问题。
配置文件大概有三种情况:

  1. 存放在当前用户目录下,不同用户不同配置com.demon.test.PropertiesUtil.PropertiesUtil.getDefaultSystemProperties(String)
  2. 存放在所有用户公共目录下,同一台电脑上所有用户共享同一个配置com.demon.test.PropertiesUtil.PropertiesUtil.getDefaultSystemProperties_AllUser(String)
  3. 放在jar包内,一个jar唯一一个com.demon.test.PropertiesUtil.PropertiesUtil.getPropertiesByResource(String)com.demon.test.PropertiesUtil.PropertiesUtil.getPropertiesByResourceAsStream(String)
    以下是正文:
package com.demon.test.PropertiesUtil;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;

/***
 * Properties工具类
 * @author demon
 *
 */
public class PropertiesUtil extends Properties{
    
    private static final long serialVersionUID = 1L;
    
    /**配置{USERPROFILE}/PK_NAME**/
    public final static String PK_NAME = "demon";
    
    /**配置{USERPROFILE}/PK_NAME/{projectName}**/
    public final File SYS_USER_PATH = new File(new File(System.getenv("USERPROFILE"),PK_NAME),new File(System.getProperty("user.dir")).getName());
    //ALLUSERSPROFILE
    public final File SYS_USER_ALLUSERSPROFILE = new File(new File(System.getenv("ALLUSERSPROFILE"),PK_NAME),new File(System.getProperty("user.dir")).getName());
    /**
     * 返回在程序位置的配置文件
     * @param propertiesName
     * @return new File(SYS_USER_PATH,propertiesName);
     */
    public File getDefaultSystemProperties(String propertiesName) {
        if(SYS_USER_PATH.exists()) {
            return new File(SYS_USER_PATH,propertiesName);
        }
        if(SYS_USER_PATH.mkdirs())
            return new File(SYS_USER_PATH,propertiesName);
        else
            throw new RuntimeException(SYS_USER_PATH.getAbsolutePath() + "创建失败");
    }
    public File getDefaultSystemProperties_AllUser(String propertiesName) {
        if(SYS_USER_ALLUSERSPROFILE.exists()) {
            return new File(SYS_USER_ALLUSERSPROFILE,propertiesName);
        }
        if(SYS_USER_ALLUSERSPROFILE.mkdirs())
            return new File(SYS_USER_ALLUSERSPROFILE,propertiesName);
        else
            throw new RuntimeException(SYS_USER_ALLUSERSPROFILE.getAbsolutePath() + "创建失败");
    }
    /**
     * 返回项目内部资源文件的配置文件
     * @param name
     * @return
     */
    public String getPropertiesByResource(String name) {
        return this.getClass().getResource('/'+name).getFile();
    }
    /***
     * 返回项目内部资源文件的配置文件
     * @param name
     * @return
     */
    public InputStream getPropertiesByResourceAsStream(String name) {
        return this.getClass().getResourceAsStream('/'+name);
    }
    public PropertiesUtil() {
        super();
    }
    public PropertiesUtil(Properties pro) {
        super(pro);
    }
    public static void main(String[] args) {

        File file = new PropertiesUtil().getDefaultSystemProperties("propertiesName");
        System.out.println(file.getAbsolutePath());
        file = new PropertiesUtil().getDefaultSystemProperties_AllUser("propertiesName");
        System.out.println(file.getAbsolutePath());
        System.out.println(new PropertiesUtil().getPropertiesByResource("test.properties"));
        new PropertiesUtil().getPropertiesByResourceAsStream("test.properties");
    }
}

输出:

C:\Users\hp\demon\PropertiesUtil\propertiesName
C:\ProgramData\demon\PropertiesUtil\propertiesName
/E:/eclipse-workspace/PropertiesUtil/bin/test.properties
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,242评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,999评论 6 342
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,834评论 18 399
  • 昨晚接了快递的电话,我和孩子说咱的书回来了,没想到她竟会欢呼雀跃起来!我的天,一箱子啊,从来没这样买过书!拆开放好...
    韩芬阅读 276评论 0 0
  • 参加火星人公益放生活动!今天去了金沙江参加放生,大家先集合,然后走路去市场买泥鳅,走路去江边放泥鳅!今天来的人更多...
    耕读传家林亮伟阅读 280评论 0 2