Home > Java > javaTutorial > body text

Java read Properties file instance method

PHPz
Release: 2017-04-23 14:46:22
Original
1590 people have browsed it

This blog post will introduce using java to read Properties files. I recently came across a new project with a large number of property files, so I came up with the idea of ​​writing this blog post. Regarding properties, they are generally used to store data files in the form of key-value pairs. For example, we often put Jdbc's configuration file in the properties file, and then configure it in xml In the file, we directly use ${} to read the contents, such as the following code. The advantage of this is that if the password of the database changes, we only need to modify the properties file, which reduces other workload. , increasing the convenience of the program. So how does the program read the contents of the file ? This is the question this blog post will discuss:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		
		<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
	</bean>
Copy after login
//properties文件配置数据库的内容:
jdbc.username=root
jdbc.password=1230
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///test
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
Copy after login

Okay, let’s get to the point, reading the Properties file , let’s first take a look at the API of the Properties class, which mainly uses the following methods:

<strong>getProperty</strong>(String key) Use the specified Key Search property

<strong>load</strong>(InputStream inStream) from the input stream Read the attribute list (key and element pairs)

Without further ado, let’s get to the code. We use the code to read the JDBC described aboveConnect to the file of database and retrieve the username and password:

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

public class ReadPropertiesFile {

	public static void main(String[] args)throws Exception {

		readProperties();

	}

	private static void readProperties() throws IOException{

		Properties pro = new Properties(); //建立一个映射的properties类

		InputStream in=null;

		try {

			in = new BufferedInputStream(new FileInputStream("DataBaseConfig.properties"));//根据文件获取流

			pro.load(in);//加载流

			Set<String> contents = pro.stringPropertyNames();  

			Iterator<String> iter = contents.iterator();

			while (iter.hasNext()) {

				String key = iter.next();

				if (key.indexOf("username")!=-1) {

					System.out.println("数据库的用户名是:"+pro.getProperty(key));

				}

				if (key.indexOf("password")!=-1) {  

					System.out.println("数据库的连接密码:"+pro.getProperty(key));

				}

			}

		} catch (Exception e) {

			e.printStackTrace();

			System.err.println("读取过程发生异常");
		}finally {

			if (in!=null) { //一定要关闭流

				in.close();
			}
		}

	}

}
//输出结果:
Copy after login

The username of the database is: root
The connection password of the database:1230

3. This is my project structure. It must be noted that the properties file must be under the main directory of the project, otherwise an exception will be thrown

 

## Okay, that’s it for this blog post. Welcome everyone’s advice. If you are interested, please study

php Chinese website online java video tutorial

The above is the detailed content of Java read Properties file instance method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template