JAVA读取配置文件工具类

package cn.ac.iscas.utils;

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

public class PropertiesUtil {

    public static Properties prop = new Properties();

    public static Object getProperty(String name) {
        return prop.get(name);
    }

    /**
     * 加载配置文件kjdj.properties
     */
    public static void initProperties() {
        String sateudp = System.getProperty("user.dir") + File.separator + "kjdj.properties";
        try {
            prop.load(new FileInputStream(new File(sateudp)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getString(String name) {
        initProperties();
        Object value = prop.get(name);
        if (value != null) {
            return value.toString();
        }
        return "";
    }

    public static int getInt(String name) {
        initProperties();
        Object value = prop.get(name);
        if (value != null) {
            return Integer.parseInt(value.toString());
        }
        return 0;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值