Java Properties 文件的加载和配置管理

本文介绍了一个用于加载和管理属性文件的Java实用工具类。包括从类路径和文件路径加载属性文件的方法,并提供了获取不同类型的属性值的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、属性文件加载类


import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesUtils {

    /**
     * Load properties file from classpath.
     */
    public static Properties loadPropertiesResource(String aResourcePath) throws IOException {
        try {
            ClassLoader classLoader = PropertiesUtils.class.getClassLoader();

            Properties properties = new Properties();

            // Try loading it.
            properties.load(classLoader.getResourceAsStream(aResourcePath));
            return properties;
        } catch (Throwable t) {
            throw new IOException("failed loading Properties resource from " + aResourcePath);
        }
    }

    /**
     * Load properties file from file path.
     */
    static public Properties loadPropertiesFile(String aFilePath) throws IOException {
        try {

            Properties properties = new Properties();

            properties.load(new FileInputStream(aFilePath));
            return properties;
        } catch (Throwable t) {
            throw new IOException("failed loading Properties file from " + aFilePath);
        }
    }

    /**
     * Shorthand for current time.
     */
    static public long now() {
        return System.currentTimeMillis();
    }

}

二、属性文件管理类


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


public class Config {

    private static final String PROPERTIES_FILE = "config.properties";
    private static Properties properties;

    private Config() throws Exception{
        throw new Exception("Config cant be init");
    }

    /**
     * Initialize event sources from properties file.
     */
    public static void load(String aDirPath) {
        // Load Event sources using properties file.
        try {
            properties = PropertiesUtils.loadPropertiesResource(PROPERTIES_FILE);
        } catch (Throwable t) {
            String filePath = aDirPath + File.separator + PROPERTIES_FILE;
            try {
                properties = PropertiesUtils.loadPropertiesFile(filePath);
            } catch (Throwable t2) {
                return;
            }
        }
    }

    public static String getProperty(String aName, String aDefault) {
        return properties.getProperty(aName, aDefault);
    }

    public static String getProperty(String aName) {
        String value = properties.getProperty(aName);
        if (value == null) {
            throw new IllegalArgumentException("Unknown property: " + aName);
        }
        return value;
    }

    public static boolean getBoolProperty(String aName) {
        String value = getProperty(aName);
        try {
            return value.equals("true");
        } catch (Throwable t) {
            throw new IllegalArgumentException("Illegal property value: " + aName + " val=" + value);
        }
    }

    public static int getIntProperty(String aName) {
        String value = getProperty(aName);
        try {
            return Integer.parseInt(value);
        } catch (Throwable t) {
            throw new IllegalArgumentException("Illegal property value: " + aName + " val=" + value);
        }
    }

    public static long getLongProperty(String aName) {
        String value = getProperty(aName);
        try {
            return Long.parseLong(value);
        } catch (Throwable t) {
            throw new IllegalArgumentException("Illegal property value: " + aName + " val=" + value);
        }
    }

    public static boolean hasProperty(String aName) {
        return properties.containsKey(aName);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值