java读取配置文件工具类

import org.hibernate.engine.jdbc.StreamUtils;

import java.io.*;
import java.util.Date;
import java.util.Properties;

public class PropertiesUtil {
    // 配置文件
    // 配置文件路径
    private static final String confFile = "server/runtime/jdzzReport.properties";
    // 用到的参数配置信息
    private static String API_CODE = null;
    private static String BUSINESS_TYPE = null;
    private static String REPAIRMARK = null;
    private static String USER = null;
    private static String PASSWORD = null;
    private static String reporturl = null;
    private static String DATA_FLAG = null;
    private static String DATA_TYPE = null;


    //    private static String token = "";
//    private static String refreshToken = "";
//     token过期时间,以秒为单位
//    private static Date tokenExpiredTime = new Date();
    // refreshToken过期时间,以秒为单位
//    private static Date refTokenExpiredTime = new Date();
    private static long fileLastModified = 0;

    /**
     * 检查文件是否更新过
     *
     * @return
     */
    private static boolean checkFileUpdated() {
        File conf = new File(confFile);
        if (!conf.exists()) {
            throw new RuntimeException("配置文件不存在" + conf.getAbsolutePath());
        }
        long latest = conf.lastModified();
        if (latest > fileLastModified) {
            fileLastModified = latest;
            return true;
        }
        return false;
    }

    private static void readProperties() {
        File conf = new File(confFile);
        if (!conf.exists()) {
            throw new RuntimeException("配置文件不存在" + conf.getAbsolutePath());
        }
        Properties properties = new Properties();

        try {
            FileInputStream confInput = new FileInputStream(conf);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            StreamUtils.copy(confInput, byteArrayOutputStream);
            String confText = new String(byteArrayOutputStream.toByteArray(), "utf8");
            properties.load(new StringReader(confText));
            //关闭输入流
            confInput.close();

            API_CODE = properties.getProperty("API_CODE");
            BUSINESS_TYPE = properties.getProperty("BUSINESS_TYPE");
            REPAIRMARK = properties.getProperty("REPAIRMARK");
            USER = properties.getProperty("USER");
            PASSWORD = properties.getProperty("PASSWORD");
            reporturl = properties.getProperty("reporturl");
            DATA_FLAG = properties.getProperty("DATA_FLAG");
            DATA_TYPE = properties.getProperty("DATA_TYPE");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //生成的参数get方法
    public static String getApiCode() {
        if (API_CODE == null || API_CODE.equals("") || checkFileUpdated()) readProperties();
        return API_CODE;
    }


    public static String getBusinessType() {
        if (BUSINESS_TYPE == null || BUSINESS_TYPE.equals("") || checkFileUpdated()) readProperties();
        return BUSINESS_TYPE;
    }


    public static String getREPAIRMARK() {
        if (REPAIRMARK == null || REPAIRMARK.equals("") || checkFileUpdated()) readProperties();
        return REPAIRMARK;
    }


    public static String getUSER() {
        if (USER == null || USER.equals("") || checkFileUpdated()) readProperties();
        return USER;
    }


    public static String getPASSWORD() {
        if (PASSWORD == null || PASSWORD.equals("") || checkFileUpdated()) readProperties();
        return PASSWORD;
    }


    public static String getReporturl() {
        if (reporturl == null || reporturl.equals("") || checkFileUpdated()) readProperties();
        return reporturl;
    }


    public static String getDataFlag() {
        if (DATA_FLAG == null || DATA_FLAG.equals("") || checkFileUpdated()) readProperties();
        return DATA_FLAG;
    }

    public static String getDataType() {
        if (DATA_TYPE == null || DATA_TYPE.equals("") || checkFileUpdated()) readProperties();
        return DATA_TYPE;
    }

}

使用时直接调用静态的get方法即可,例如:

PropertiesUtil.getUSER()

配置文件中的形式为key=value的形式,例如:USER = zhangsan

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值