获取java web项目里面所有的properties文件并组装在一起,使用方便,配置简单



今天写了一个类,用来加载当前项目下的所有的properties配置文件


项目文件结构及使用方式


结构

--resources

--order.properties

--product.properties

--kafka.properties

--zk.properties


简单的使用方式

String val = PropertyUtil.get("order.key");

String val = PropertyUtil.get("product.key");

String val = PropertyUtil.get("kafka.key");

String val = PropertyUtil.get("zk.key");

通过 文件名.key 的方式获取配置好的 value


以后再也不用再每个配置文件单独去加载获取里面的内容了,解放配置文件的使用。


如果是非web项目,也可以完成同样的功能,只需要把根目录指向 ‘user.dir’即可




具体代码如下


package xxx.framework.basic;

import java.io.*;
import java.util.*;

/**
 * Created by ikong on 2016-06-28.
 */
public class PropertyUtil {

    private static String fileExtension = ".properties";

    private static Properties properties = null;

    static {
        init();
    }

    public static String get(String key) {
        return properties.getProperty(key);
    }

    public static void init() {
        properties = new Properties();

        String path = PropertyUtil.class.getClassLoader().getResource("/").getPath();

        File file = new File(path);

        List<File> files = getPropFile(file);

        Map<String, File> map = new HashMap<String, File>();

        for (File item : files) {
            String fileName = item.getName().replace(fileExtension, "");
            map.put(fileName, item);//去重
            System.out.println(item.getName());

            try {
                FileReader reader = new FileReader(item.getPath());
                BufferedReader br = new BufferedReader(reader);
                String line;
                while ((line = br.readLine()) != null) {
                    int index = line.indexOf("=");
                    if (index < 0) {
                        continue;
                    }
                    String name = line.substring(0, index);
                    String val = line.substring(index + 1);
                    properties.put(fileName + "." + name, val);
                }
                br.close();
                reader.close();

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

    public static List<File> getPropFile(File file) {
        List<File> list = new ArrayList<File>();
        File[] files = file.listFiles();
        for (File fileItem : files) {
            if (fileItem.isDirectory()) {
                List<File> items = getPropFile(fileItem);
                list.addAll(items);
            } else if (fileItem.getName().endsWith(fileExtension)) {
                list.add(fileItem);
            }
        }
        return list;
    }
}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码者人生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值