package com.mes.tools;
|
|
import java.io.FileNotFoundException;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.util.Properties;
|
|
/**
|
* @author SNG-012
|
*
|
* 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);
|
}
|
}
|