关于 读取 resource.propety 文件 工具文件

本文详细解析了Java中通过Properties类读取配置文件的原理及实现过程,包括资源路径获取、文件输入流操作、异常处理和资源值获取等关键步骤。重点介绍了三种不同的配置文件读取方式,以及如何在不同场景下灵活应用。

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

package com.xiaoma.util;


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


public class SysUtil {
    static String resourceconfigPath = "";


    public static Properties getProperties(String filefullname) {
        FileInputStream fis = null;
        try {
            File file = new File(filefullname);
            Properties properties = new Properties();
            fis = new FileInputStream(file);
            properties.load(fis);
            return properties;
        } catch (Exception e) {
            // logger.error(e);
            return null;
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e) {
                return null;
            }
        }
    }


    public static String getResourceconfigPath() {
        if (resourceconfigPath.equals("")) {
            String classPath = SysUtil.class.getClassLoader()
                    .getResource(SysUtil.class.getName().replace('.', '/') + ".class").getFile();
            String classRoot = "";
            classRoot = classPath.substring(0, classPath.indexOf("/com/xiaoma/bank/util"));
            if (!classRoot.equals("") && classRoot.startsWith("/") && classRoot.indexOf(":") == 2) {
                classRoot = classRoot.substring(1);
            }
            // this.getClass().getClassLoader().getResource("rule.xml").getPath();
            if (!classRoot.equals("")) {
                resourceconfigPath = classRoot + "/resource.properties";
            }
        }
        return resourceconfigPath;
    }


    public static String getResourceValue(String key) {
        Properties resourceProperties = SysUtil.getProperties(getResourceconfigPath());
        return resourceProperties.getProperty(key);
    }


}

 

//第一种 可以放在 自己的定义的路径下

 

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


public class dfsfd{
    private static Properties props = new Properties();
    static {
        try {
            InputStream in = WebConf.class.getResourceAsStream("/appConfig.properties");
            props.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getValue(String key) {
        try {
            String value = props.getProperty(key);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public static void main(String[] args) {
        System.out.println(WebConf.getValue("serviceBaseUrl"));
    }

}

//第二种 放在 resource 下

 

package com.xiaoma.utils;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;

public class Constants {
    private static String configFile = "/config.properties";

    public static String unionPay_pinBlockKey;
    static {
        Properties properties = new Properties();
        InputStream in = null;
        String value = null;
        String fieldName = null;
        String fieldType = null;
        Method method = null;
        try {
            in = Constants.class.getResourceAsStream(configFile);
            properties.load(in);
            Class<Constants> clazz = Constants.class;
            Constants obj = clazz.newInstance();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                fieldName = field.getName();
                if (properties.containsKey(fieldName)) {
                    method = Properties.class.getDeclaredMethod("getProperty", String.class);
                    value = (String) method.invoke(properties, fieldName);
                    value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
                    if (null != value) {
                        fieldType = field.getType().getName();
                        if (String.class.getName().equals(fieldType)) {
                            field.set(obj, value);
                        } else if ("int".equals(fieldType)) {
                            field.setInt(obj, Integer.parseInt(value));
                        } else if ("boolean".equals(fieldType)) {
                            field.setBoolean(obj, Boolean.parseBoolean(value));
                        } else {
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("载入配置文件" + configFile + "时发生错误");
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 私有构造函数,仅允许Constants在初始化成员变量时创建实例,不允许其他类调用
     */
    private Constants() {
    }
}

 

第三种也得放在 resource 下 property 文件

 


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值