Let's take a look at the established Test Engineering catalogue
Property file We put it under the package test, of course, generally in the actual development process is not recommended to do so, the establishment of the attribute file in the SRC directory, now placed under the package is mainly easy to understand the path of the problem.
Let's take a look at the code that reads the property file, and the property file configures a class Hello K-v key value that we're going to read and load into memory.
Readproperties.properties
V=com.luhy.test.hello
Hello class:
Package com.luhy.test;
public class Hello {public
void run () {
System.out.println ("Hello");
}
Readproperties.java
Package com.luhy.test;
Import java.util.Properties;
public class ReadProperties {public
static void Main (string[] args) throws exception{
String filename = "Com/luhy /test/readproperties.properties ";
Properties Props = new properties ();
Props.load (ReadProperties.class.getClassLoader (). getResourceAsStream (filename));
String h = props.getproperty ("V");
Object o = Class.forName (h). newinstance ();
Hello hello = (hello) o;
Hello.run ();
}
Finish Print out:
Hello
Let's look at the compiled bin directory
Visible after the compilation of the property file is automatically placed in the appropriate package, of course, where the bin is equivalent to the source in the SRC, the actual development in general placed in this SRC directory, so in the release of the project will not have to toss.
Description
1, Props.load (ReadProperties.class.getClassLoader (). getResourceAsStream (filename)); It means to get the class loader from the properties class (the class loader has four main types, loading different classes separately, loading the class file into memory and not producing objects), and converting the specified file into a stream. This step, there are a lot of novice, directly to the load () fill in the file name or file path name, the program will not be able to run error found the specified path. Therefore, we must pay attention to this point.