java loaded configuration file attributes (properties file) - from entry to advanced

What are the properties file

  • Properties profile, .properties suffix.
  • File in strict accordance with the data key = value parameters to fill in
  • Chinese Unicode encoding parameter values ​​need to turn
  • Does not distinguish between basic data types
  • Edited a file as shown aaa.properties
username=root
flag=true
xm =\u4f60\u597d
age=18

Why use properties files

Imagine such a scenario, when you publish the project on-line, such as connecting the port number mysql database need to be adjusted, do not change the code needs to be rewritten, packaging, publishing it? For some important parameters need to artificially change the arguments often use a properties file to read, do not modify the code, and finished modifying the parameters, restart the web server will be able to achieve the expected demand, we will be back on dynamic loading properties file, even It does not require a restart.

Using java to load properties file in two ways

Using the class loader loads the file input stream is obtained

	@Test
	/**
	 * 使用类的加载器获得文件输入流
	 * @author CC
	 * @date 2020年3月24日
	 */
	public void demo1() throws IOException{
		Properties prop =new Properties();
		InputStream inputstream= TestProp.class.getClassLoader().getResourceAsStream("test/aaa.properties");
		prop.load(inputstream);
		String username=prop.getProperty("username");
		boolean flag=Boolean.valueOf(prop.getProperty("flag"));
		String xm=prop.getProperty("xm");
		int age =Integer.valueOf(prop.getProperty("age"));
		System.out.println( username + " " +flag+" "+xm+" "+age);		
	}

Need some attention:

  • Properties class is a class in the java.util package, while a Map implementation class, can be used directly, will have the opportunity to introduce!
  • Acquisition parameters prop.getProperty ( "key"), this parameter value is obtained in the form of a character string, conversion needs demand
  • Chinese does not require special handling, Unicode encoding will be converted into Chinese
  • Junit unit using the following test results:
root true 你好 123

getResourceAsStream () Introduction

thereof as described in jdk: Returns an input stream for reading the specified resource.

public InputStream getResourceAsStream(String name)

How to determine it this time specified resource path?
If it is java project it is relative to the path of src
, such as: is there a aaa.properties file located in the root directory of src, add the correct access as follows:

getResourceAsStream("aaa.properties");

For example: is there a test folder located in the root directory of src, which has a aaa.properties file, add the correct access as follows:

getResourceAsStream("test/aaa.properties");

Load the file using a file input stream

	@Test
	/**
	 * 使用标准的文件输入流读取文件
	 * @author CC
	 * @date 2020年3月24日
	 */
	public void demo2() throws IOException{
		Properties prop =new Properties();
		InputStream inputstream= new FileInputStream("src/test/aaa.properties");
		prop.load(inputstream);
		String username=prop.getProperty("username");
		boolean flag=Boolean.valueOf(prop.getProperty("flag"));
		String xm=prop.getProperty("xm");
		int age =Integer.valueOf(prop.getProperty("age"));
		System.out.println( username + " " +flag+" "+xm+" "+age);		
	}

What disadvantage of using file input stream is?
Relative path of the file to determine the bad
one: If you are a java project, so you can access:

 new FileInputStream("src/test/aaa.properties");

Two: If you are a web project, we know works in the Tomcat webapps is no src path, all the src files are compiled under the WEB-INF folder in the classes below, is clearly above this access will be reported FileNotFound exception

The kind of dynamic loading properties file without restarting

Scene: already published web project, I want to modify the parameters do not want to restart how to do it? The above conventional manner using a class loader input stream is obtained, has the following disadvantages:
After modifying .properties, even when re-executed, the read parameters before modification remains. After the cause of this problem is that ClassLoader.getResourceAsStream read, .properties will be stored in the cache, reads from the cache when re-executed, instead of reading the .properties file again

Use FileInputStream acquired from the disk every time the absolute path of the file attributes, you can solve the aforementioned disadvantages, but what needs to obtain an absolute path to a file?

		Properties prop =new Properties();
		//InputStream inputstream= ImgCheckServlet.class.getClassLoader().getResourceAsStream("aaa.properties");
		//获取绝对路径
		String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		System.out.println(path);
		InputStream inputstream= new FileInputStream(path+"/aaa.properties");
		prop.load(inputstream);
		String username=prop.getProperty("username");
		boolean flag=Boolean.valueOf(prop.getProperty("flag"));
		String xm=prop.getProperty("xm");
		int age =Integer.valueOf(prop.getProperty("age"));
		System.out.println( username + " " +flag+" "+xm+" "+age);

You can look at a servlet call under the effect of the web project: for example, we try to change age = 123, the console can see the effect as follows, with immediate effect

root true 你好 123
Published 33 original articles · won praise 40 · views 20000 +

Guess you like

Origin blog.csdn.net/cchulu/article/details/105084003