import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .*;
/**
* @desc: How Properties reads the property values of configuration files
*/
public class PropertiesTest {
/**
* 1. Method 1
* Get from the getResourcesAsStream of the current class loader
* InputStream inputStream = ().getResourceAsStream(name)
*
* @throws IOException
*/
@Test
public void test1() throws IOException {
InputStream inputStream = ().getResourceAsStream("");
Properties properties = new Properties();
(inputStream);
();
("==============================================");
String property = ("");
("property = " + property);
}
/**
* 2. Method 2
* Get from the getResourcesAsStream of the current class loader
* InputStream inputStream = ().getClassLoader().getResourceAsStream(name)
*
* @throws IOException
*/
@Test
public void test5() throws IOException {
InputStream inputStream = ().getClassLoader().getResourceAsStream("config/");
Properties properties = new Properties();
(inputStream);
();
("==============================================");
String property = ("");
("property = " + property);
}
/**
* 3. Method Three
* Use the getSystemResourceAsStream method of Class class and the ClassLoader of the current class is the same
* InputStream inputStream = (name)
*
* @throws IOException
*/
@Test
public void test4() throws IOException {
InputStream inputStream = ("config/");
Properties properties = new Properties();
(inputStream);
();
("==============================================");
String property = ("");
("property = " + property);
}
/**
* 4. Method 4
* Resource resource = new ClassPathResource(path)
*
* @throws IOException
*/
@Test
public void test2() throws IOException {
Resource resource = new ClassPathResource("config/");
Properties properties = (resource);
();
("==============================================");
String property = ("");
("property = " + property);
}
/**
* 5. Method 5
* Getting from the file and using InputStream bytes is mainly necessary to add the src directory address of the project where the current configuration file is located. The path configuration needs to be accurate to the absolute address level
* BufferedInputStream inherits from InputStream
* InputStream inputStream = new BufferedInputStream(new FileInputStream(name)
* This method requires a complete path. The advantage is that it can read files under any path, but the disadvantage is that it is not very flexible.
* @throws IOException
*/
@Test
public void test3() throws IOException {
InputStream inputStream = new BufferedInputStream(new FileInputStream("src/main/resources/config/"));
Properties properties = new Properties();
(inputStream);
();
("==============================================");
String property = ("");
("property = " + property);
}
/**
* 6. Way 6
* Getting from the file and using InputStream bytes is mainly necessary to add the src directory address of the project where the current configuration file is located. The path configuration needs to be accurate to the absolute address level
* FileInputStream inherits from InputStream
* InputStream inputStream = new FileInputStream(name)
* This method requires a complete path. The advantage is that it can read files under any path, but the disadvantage is that it is not very flexible.
* @throws IOException
*/
@Test
public void test6() throws IOException {
InputStream inputStream = new FileInputStream("src/main/resources/config/");
Properties properties = new Properties();
(inputStream);
();
("==============================================");
String property = ("");
("property = " + property);
}
/**
* 7. Method 7
* Use the InputStream stream to operate the ResourceBundle, and the methods to obtain the stream are as follows.
* ResourceBundle resourceBundle = new PropertyResourceBundle(inputStream);
* @throws IOException
*/
@Test
public void test7() throws IOException {
InputStream inputStream = ("config/");
ResourceBundle resourceBundle = new PropertyResourceBundle(inputStream);
Enumeration<String> keys = ();
while (()) {
String s = ();
(s + " = " + (s));
}
}
/**
* 8. Way eight
* The path access is similar to: it is read from the root directory by default, and the files in the resources directory can also be read.
* ResourceBundle rb = ("b") //No need to specify the suffix of the file name, just write the file name prefix
*/
@Test
public void test8(){
//ResourceBundle rb = ("jdbc"); //Read the resources directory
ResourceBundle rb2 = ("config/application");//Read the resources/config directory
for(String key: ()){
String value = (key);
(key + ":" + value);
}
}
/**
* A separate extraction method, user detection can correctly manipulate Properties
*
* @param inputStream
* @throws IOException
*/
private void printKeyValue(InputStream inputStream) throws IOException {
Properties properties = new Properties();
(inputStream);
Set<Object> keys = ();
for (Object key : keys) {
(key + " = " + (key));
}
}
}