Java 读取 .properties 配置文件

示例:database.properties配置文件:hotelOrderRow=10driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306username=rootpassword=666666下面是以及和的形式读取配置文件中所有的属性及值package org.utils;import java...
摘要由CSDN通过智能技术生成

示例:

database.properties配置文件:

hotelOrderRow=10
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306
username=root
password=666666

下面是以及和的形式读取配置文件中所有的属性及值

package org.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

public class ReadConfigManager {

	private static ReadConfigManager readConfigManager;

	// 单实例方法
	public static synchronized ReadConfigManager getConfigManager() {
		if (readConfigManager == null) {
			readConfigManager = new ReadConfigManager();
		}
		return readConfigManager;
	}

	// 根据配置文件中属性的键获得对应的值
	public Properties getValue() {
		Properties properties = new Properties();
		String configFile = "database.properties";
		// 使用ClassLoader加载properties配置文件生成对应的输入流
		InputStream is = ReadConfigManager.class.getClassLoader().getResourceAsStream(configFile);
		try {
			// 使用properties对象加载输入流
			properties.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close(); // 关闭流
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// 获取key对应的value值
		return properties;
	}
	
	private void printAllProperty(){
		Enumeration en = getValue().propertyNames();
		while (en.hasMoreElements()) { //遍历
			String key = (String) en.nextElement(); //获取key值
			String value = getValue().getProperty(key);  //获取Value值
			System.out.println(key+"="+value);
		}
	}

	public static void main(String[] args) {
		ReadConfigManager readConfigManager = new ReadConfigManager();
		readConfigManager.printAllProperty();
		
	}
}

读取配置文件单一值(已知键)

package org.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

public class ReadConfigManager {

	private static ReadConfigManager readConfigManager;

	// 单实例方法
	public static synchronized ReadConfigManager getConfigManager() {
		if (readConfigManager == null) {
			readConfigManager = new ReadConfigManager();
		}
		return readConfigManager;
	}

	// 根据配置文件中属性的键获得对应的值
	public String getValue(String key) {
		Properties properties = new Properties();
		String configFile = "database.properties";
		// 使用ClassLoader加载properties配置文件生成对应的输入流
		InputStream is = ReadConfigManager.class.getClassLoader().getResourceAsStream(configFile);
		try {
			// 使用properties对象加载输入流
			properties.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close(); // 关闭流
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// 获取key对应的value值
		return properties.getProperty(key);
	}

	public static void main(String[] args) {
		ReadConfigManager readConfigManager = new ReadConfigManager();
		String row = readConfigManager.getValue("hotelOrderRow");
		System.out.println("hotelOrderRow = "+row);
	}
}

注意:在使用中遇到的最大的问题可能是配置文件的路径问题,如果配置文件入在当前类所在的包下,那么需要使用包名限定。

更多读取方式方法参考:https://www.cnblogs.com/sebastian-tyd/p/7895182.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值