Java读取配置文件工具类

当你的业务需要读取自定义的配置文件内容时你是不是傻眼了,我能写出和springboot那种高端的读取配置文件的工具吗???
其实很简单,参考如下代码即可解决你的疑惑:
properties文件内容如下:

com.marlon=我的英文名字叫marlon

工具类PropertiesUtil代码内容如下:

import java.io.*;
import java.net.URL;
import java.util.Properties;

/**
 * @author marlon
 * @date 2021年8月10日 11:39:34
 */
public class PropertiesUtil {
    private static Properties props = null;
    private static File configFile = null;
    private static long fileLastModified = 0L;

    private static String configFileName = "test.properties";

    private static void init() {
        URL url = ProUtil.class.getClassLoader().getResource(configFileName);
        configFile = new File(url.getFile());
        fileLastModified = configFile.lastModified();
        props = new Properties();
        load();
    }

    private static void load() {
        try {
            props.load(new InputStreamReader(new FileInputStream(configFile),"UTF-8"));
            fileLastModified = configFile.lastModified();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static String getConfig(String key) {
        if ((configFile == null) || (props == null)) init();
        if (configFile.lastModified() > fileLastModified) load(); //当检测到文件被修改时重新加载配置文件
        return props.getProperty(key);
    }

    public static void main(String[] args){
        System.out.println(getConfig("com.marlon"));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个Java配置文件分段读取工具类,可以将配置文件按照section分段读取,每个section中的键值对以Map的形式存储: ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class ConfigParser { private String filename; private Map<String, Map<String, String>> sections; public ConfigParser(String filename) throws IOException { this.filename = filename; this.sections = new HashMap<>(); parse(); } public String get(String section, String option) { Map<String, String> options = sections.get(section); if (options != null) { return options.get(option); } return null; } public void set(String section, String option, String value) { Map<String, String> options = sections.computeIfAbsent(section, k -> new HashMap<>()); options.put(option, value); } private void parse() throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(filename))) { String line; String section = null; Map<String, String> options = new HashMap<>(); while ((line = br.readLine()) != null) { line = line.trim(); if (line.matches("\\[.*\\]")) { if (section != null) { sections.put(section, options); } section = line.substring(1, line.length() - 1); options = new HashMap<>(); } else if (line.matches(".*=.*")) { String[] parts = line.split("=", 2); String option = parts[0].trim(); String value = parts[1].trim(); options.put(option, value); } } if (section != null) { sections.put(section, options); } } } } ``` 使用示例: ```java public static void main(String[] args) throws IOException { ConfigParser config = new ConfigParser("config.ini"); String value = config.get("section1", "option1"); System.out.println(value); config.set("section2", "option2", "value2"); // ... } ``` 其中,`config.ini`配置文件的格式如下: ``` [section1] option1 = value1 option2 = value2 [section2] option3 = value3 option4 = value4 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值