java 读取配置(read config from file)

本文介绍了如何使用Java从配置文件中读取ES主机和端口字符串,通过使用Properties类和资源加载方法实现配置参数的动态获取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java read config from file

public final class Config {
	public  static String ES_HOST_STRING;
	public static String ES_PORT_STRING;
	static{
		Properties prop = new Properties();
		String propFileName = "config.properties";
		InputStream inputStream = Config.class.getClassLoader().getResourceAsStream(propFileName);
		if (inputStream != null) {
			try {
				prop.load(inputStream);
				ES_HOST_STRING = prop.getProperty("loganalysis.es.host");
				ES_PORT_STRING = prop.getProperty("loganalysis.es.port");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} else {
			try {
				throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}


package com.hexiang.utils; import java.io.File; import java.io.FileInputStream; import java.util.Properties; public class ReadConfigation{ /** * 属性文件全名 */ private static final String PFILE ="Config.properties"; /** * 对应于属性文件的文件对象变量 */ private File m_file = null; /** * 属性文件的最后修改日期 */ private long m_lastModifiedTime = 0; /** * 属性文件所对应的属性对象变量 */ private Properties m_props = null; /** * 本类可能存在的惟一的一个实例 */ private static ReadConfigation m_instance =new ReadConfigation(); /** * 私有的构造子,用以保证外界无法直接实例化 */ private ReadConfigation() { m_file = new File(PFILE); m_lastModifiedTime = m_file.lastModified(); if(m_lastModifiedTime == 0){ System.err.println(PFILE +" file does not exist!"); } m_props = new Properties(); try { m_props.load(new FileInputStream(PFILE)); } catch(Exception e) { e.printStackTrace(); } } /** * 静态工厂方法 * @return 返还ReadConfigation 类的单一实例 */ synchronized public static ReadConfigation getInstance() { return m_instance; } /** * 读取一特定的属性项 * * @param name 属性项的项名 * @param defaultVal 属性项的默认值 * @return 属性项的值(如此项存在), 默认值(如此项不存在) */ public String getConfigItem(String name,String defaultVal) { long newTime = m_file.lastModified(); // 检查属性文件是否被其他程序 // 如果是,重新读取此文件 if(newTime == 0) { // 属性文件不存在 if(m_lastModifiedTime == 0){ System.err.println(PFILE+ " file does not exist!"); }else{ System.err.println(PFILE+ " file was deleted!!"); } return defaultVal; }else if(newTime > m_lastModifiedTime){ // Get rid of the old properties m_props.clear(); try { m_props.load(new FileInputStream(PFILE)); }catch(Exception e){ e.printStackTrace(); } } m_lastModifiedTime = newTime; String val = m_props.getProperty(name); if( val == null ) { return defaultVal; } else { return val; } } /** * 读取一特定的属性项 * * @param name 属性项的项名 * @return 属性项的值(如此项存在), 空(如此项不存在) */ public String getConfigItem(String name){ return getConfigItem(name,""); } }
在 Qt 框架中,读取配置文件的方法通常使用 `QSettings` 类实现。`QSettings` 提供了跨平台支持,并且可以轻松地读写应用程序的配置信息。以下是一个基于你提供的引用内容以及标准 Qt 实践的示例实现: ### 读取配置文件的实现 为了更好地组织代码并实现可重用性,可以将读取配置文件的功能封装到一个函数或类中。下面是一个简单的实现方法,展示了如何从 INI 文件中读取配置项。 ```cpp #include <QSettings> #include <QString> #include <QDebug> // 定义一个全局函数用于读取配置文件 void ReadConfigFile(const QString &filePath, QString &node1Value) { QSettings *settings = new QSettings(filePath, QSettings::IniFormat); // 读取指定路径下的配置项 node1Value = settings->value("/config/node1", "default_value").toString(); delete settings; } ``` ### 使用示例 可以在主函数或其他地方调用上述 `ReadConfigFile` 函数来读取配置文件内容: ```cpp int main() { QString configPath = "D:\\a.ini"; QString node1; ReadConfigFile(configPath, node1); qDebug() << "Node1 Value:" << node1; return 0; } ``` ### 配置文件格式(INI) 假设 `D:\\a.ini` 的内容如下: ```ini [config] node1=HelloQt ``` 运行程序后,输出结果为: ``` Node1 Value: "HelloQt" ``` ### 注意事项 - 如果配置文件不存在或没有对应键值,则 `value()` 方法会返回默认值(如示例中的 `"default_value"`)。 - 在实际项目中,建议将配置文件路径通过参数传递或者使用 `QStandardPaths` 获取系统标准路径[^3]。 - 使用完 `QSettings` 后务必释放内存,避免内存泄漏。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值