Java reads the Properties file (Read by the Properties class and ResourceBundle class)

Java reads the Properties file (Read by the Properties class and ResourceBundle class)

Tips before viewing:

The Eclipse version used in this article is Photon Release (4.8.0), and the JDK version is 1.8.0_141.

Here I provide two methods to read the properties file, Properties class and ResourceBundle class.

The directory results are as follows
Insert picture description here

Configuration file config.properties

name=zhangsan
age=18

Test class Test.java

package readProperty;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.ResourceBundle;

public class Test {
    
    

	public static void main(String[] args) throws IOException {
    
    
		
		String path = System.getProperty("user.dir") + "\\src\\readProperty\\config.properties";
		System.out.println(path);
		
		//1.Properties类
		System.out.println("--------------------------Properties--------------------------");
		Properties p = new Properties();
		p.load(new FileInputStream(path));
		System.out.println("name : " + p.get("name"));
		System.out.println("age : " + p.get("age"));
		
		//2.ResourceBundle类,此方法配置文件必须在src目录下(包括其子目录)
		System.out.println("--------------------------ResourceBundle--------------------------");
		ResourceBundle rb = ResourceBundle.getBundle("readProperty.config");
		System.out.println("name : " + rb.getString("name"));
		System.out.println("age : " + rb.getString("age"));
	}
}

The results are as follows
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43611145/article/details/105393384