Description: Upload.properties property file under Resources
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Properties;
Import Java.util.ResourceBundle;
public class Test {
private static Properties pro;
static{
InputStream InputStream = Test.class.getClassLoader (). getResourceAsStream ("upload.properties");
try {
pro= new Properties ();
Pro.load (InputStream);
} catch (IOException e) {
E.printstacktrace ();
}
}
public static Properties GetProperties () {
return pro;
}
The first way
@org. junit.test
public void Test () {
InputStream InputStream = Test.class.getClassLoader (). getResourceAsStream ("upload.properties");
Properties Pro = new properties ();
try {
Pro.load (InputStream);
String P1 = Pro.getproperty ("Repository_path");
String P2 = pro.getproperty ("Image_base_url");
System.out.println (p1);
System.out.println (p2);
} catch (IOException e) {
E.printstacktrace ();
}
}
When the JVM is started, the class is loaded and the properties file is obtained through a static method, so that the configuration in the properties file can be obtained anywhere in a project
@org. junit.test
public void Test1 () {
Properties Properties = Test.getproperties ();
String P1 = Properties.getproperty ("Repository_path");
Tring P2 = properties.getproperty ("Image_base_url");
System.out.println ("P1:" +p1);
System.out.println ("P2:" +P2);
}
The second way
@org. junit.test
public void Test2 () {
InputStream stream = Classloader.getsystemresourceasstream ("upload.properties");
Properties Pro = new properties ();
try {
Pro.load (stream);
String p = pro.getproperty ("Repository_path");
SYSTEM.OUT.PRINTLN (P);
} catch (IOException e) {
E.printstacktrace ();
}
}
The Third Way
@org. junit.test
public void Test3 () {
ResourceBundle bundle = Resourcebundle.getbundle ("Upload");
Tring string = bundle.getstring ("Repository_path");
System.out.println ("string:" +string);
}
}
Java read using the Java.util class properties to read the Properties property file under Resource