读取Propertis

读取Propertis

/**
 * 类路径 test.properties
 * @author wqzhong
 *
 */
public class PropertiesHandler {
	private static Logger logger = LoggerFactory.getLogger(PropertiesHandler.class);

	public static void main(String[] args) throws Exception {
		method01();
	}
	
	/**
	 * 基于InputStream读取配置文件
	 * 		问题:中文乱码,method02可以解决
	 * @throws Exception
	 */
	public static void method01() throws Exception {
		Properties properties = new Properties();
		InputStream inputStream = PropertiesHandler.class.getResourceAsStream("/test.properties");
		properties.load(inputStream);
		Enumeration<String> en = (Enumeration<String>) properties.propertyNames();
		getPropertisData(properties);
	}
	
	/**
	 * 解决method01()方法中文乱码问题
	 * @throws Exception
	 */
	public static void method02() throws Exception {
		Properties properties = new Properties();
		InputStream inputStream = PropertiesHandler.class.getResourceAsStream("/test.properties");
		BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
		properties.load(br);
		getPropertisData(properties);
	}
	
	/**
	 * 通过Spring中的PropertiesLoaderUtils工具类进行获取
	 * @throws Exception
	 */
	public static void method03() throws Exception {
		Properties properties = PropertiesLoaderUtils.loadAllProperties("test.properties");
		Enumeration<String> en = (Enumeration<String>) properties.propertyNames();
		while(en.hasMoreElements()) {
			String key = en.nextElement();
			String value = (String) properties.get(key);
			value = new String(value.getBytes("ISO-8859-1"),"UTF-8");
			logger.info(key+" : "+value);
		}
	}
	
	/**
	 * java.util.ResourceBundle 类读取:
	 * @throws Exception
	 */
	public static void method04() throws Exception {
		ResourceBundle bundle = ResourceBundle.getBundle("test");
		Enumeration<String> en = bundle.getKeys();
		while(en.hasMoreElements()) {
			String key = en.nextElement();
			String value = bundle.getString(key);
			value = new String(value.getBytes("ISO-8859-1"),"UTF-8");
			logger.info(key+" : "+value);
		}
	}
	
	public static void getPropertisData(Properties properties) throws UnsupportedEncodingException{
		Enumeration<String> en = (Enumeration<String>) properties.propertyNames();
		while(en.hasMoreElements()) {
			String key = en.nextElement();
			String value = (String) properties.get(key);
			logger.info(key+" : "+value);
		}
	}
}```
/**
 * 类路径 test.properties
 * @author wqzhong
 *
 */
public class PropertiesHandler {
	private static Logger logger = LoggerFactory.getLogger(PropertiesHandler.class);

	public static void main(String[] args) throws Exception {
		method01();
	}
	
	/**
	 * 基于InputStream读取配置文件
	 * 		问题:中文乱码,method02可以解决
	 * @throws Exception
	 */
	public static void method01() throws Exception {
		Properties properties = new Properties();
		InputStream inputStream = PropertiesHandler.class.getResourceAsStream("/test.properties");
		properties.load(inputStream);
		Enumeration<String> en = (Enumeration<String>) properties.propertyNames();
		getPropertisData(properties);
	}
	
	/**
	 * 解决method01()方法中文乱码问题
	 * @throws Exception
	 */
	public static void method02() throws Exception {
		Properties properties = new Properties();
		InputStream inputStream = PropertiesHandler.class.getResourceAsStream("/test.properties");
		BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
		properties.load(br);
		getPropertisData(properties);
	}
	
	/**
	 * 通过Spring中的PropertiesLoaderUtils工具类进行获取
	 * @throws Exception
	 */
	public static void method03() throws Exception {
		Properties properties = PropertiesLoaderUtils.loadAllProperties("test.properties");
		Enumeration<String> en = (Enumeration<String>) properties.propertyNames();
		while(en.hasMoreElements()) {
			String key = en.nextElement();
			String value = (String) properties.get(key);
			value = new String(value.getBytes("ISO-8859-1"),"UTF-8");
			logger.info(key+" : "+value);
		}
	}
	
	/**
	 * java.util.ResourceBundle 类读取:
	 * @throws Exception
	 */
	public static void method04() throws Exception {
		ResourceBundle bundle = ResourceBundle.getBundle("test");
		Enumeration<String> en = bundle.getKeys();
		while(en.hasMoreElements()) {
			String key = en.nextElement();
			String value = bundle.getString(key);
			value = new String(value.getBytes("ISO-8859-1"),"UTF-8");
			logger.info(key+" : "+value);
		}
	}
	
	public static void getPropertisData(Properties properties) throws UnsupportedEncodingException{
		Enumeration<String> en = (Enumeration<String>) properties.propertyNames();
		while(en.hasMoreElements()) {
			String key = en.nextElement();
			String value = (String) properties.get(key);
			logger.info(key+" : "+value);
		}
	}
}
在 Spring Boot 中,可以通过 `application.properties` 或 `application.yml` 文件来配置 Oracle 数据库连接。 下面是一个示例 `application.properties` 文件: ```properties # 数据源配置 spring.datasource.url=jdbc:oracle:thin:@localhost:1521:ORCL spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver # Hibernate 配置 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect ``` 在上面的配置中,我们指定了 Oracle 数据库的连接 URL、用户名、密码和驱动程序的类名。我们还指定了 Hibernate 的配置,包括将数据库架构更新为最新的版本、显示 SQL 查询语句以及使用 Oracle 10g 方言。 需要注意的是,如果你使用的是 `application.yml` 文件,那么配置内容应该如下所示: ```yaml # 数据源配置 spring: datasource: url: jdbc:oracle:thin:@localhost:1521:ORCL username: your_username password: your_password driver-class-name: oracle.jdbc.driver.OracleDriver # Hibernate 配置 spring: jpa: hibernate: ddl-auto: update show-sql: true properties: hibernate: dialect: org.hibernate.dialect.Oracle10gDialect ``` 无论你使用哪种方式,都应该把 Oracle 驱动程序添加到你的项目依赖中,例如: ```xml <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc8</artifactId> <version>19.3.0.0</version> </dependency> ``` 这样就完成了 Oracle 数据库的配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值