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

你可能感兴趣的:(PropertiesUtil)