Java read using the Java.util class properties to read the Properties property file under Resource

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.