wuyouming666
2024-04-18 e8d3676793d4194485afec7940aaf355af594901
hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/tools/Configuration.java
New file
@@ -0,0 +1,46 @@
package com.mes.tools;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
 * @author SNG-012
 * <p>
 * Configuration 类用于加载配置文件并提供访问配置项的方法。
 */
public class Configuration {
    /**
     * // Properties 对象,用于存储配置文件中的键值对。
     */
    private Properties properties;
    /**
     * @param fileName
     * @throws IOException // 构造函数,根据传入的文件名加载配置文件。
     */
    public Configuration(String fileName) throws IOException {
        // 使用类加载器获取资源文件的输入流。
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName);
        // 如果输入流为null,表示未找到文件,抛出FileNotFoundException。
        if (inputStream == null) {
            throw new FileNotFoundException("Property file '" + fileName + "' not found in the classpath");
        }
        // 实例化Properties对象。
        properties = new Properties();
        // 从输入流加载配置项。
        properties.load(inputStream);
    }
    /**
     * @param key
     * @return // 根据配置项的键获取其对应的值。
     */
    public String getProperty(String key) {
        return properties.getProperty(key);
    }
}