Home  >  Article  >  Java  >  Java read Properties file instance method

Java read Properties file instance method

PHPz
PHPzOriginal
2017-04-23 14:46:221442browse

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:


		
		
		
		
		
		
		
	
//properties文件配置数据库的内容:
jdbc.username=root
jdbc.password=1230
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///test
jdbc.initPoolSize=5
jdbc.maxPoolSize=10

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:

getProperty(String key) Use the specified Key Search property

load(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 contents = pro.stringPropertyNames();  

			Iterator 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();
			}
		}

	}

}
//输出结果:

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!

Statement:
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