Load resource files through the Class Loader

Source: Internet
Author: User

/*************************************** * ** Method 1 ********************************* ******************************/

Public class demo {
// The resource file can be loaded to the memory by means of a Class Loader. The advantage of this method is that the program does not need to specify the specific directory of the configuration file. The program can automatically
// Search for the file in the src directory and load the file
// Another drawback of using the following method is that the configuration file loaded by the class loader cannot be too large.
// Also, the Class Loader loads the class only once, so when the class is loaded into the memory, if someone modifies the configuration information in the configuration file, then access
// When the configuration information is not changed, it is still the original one. If we want to get the updated configuration information, we cannot use this method to load the configuration information.
Private Static Properties prop = new properties ();
Static {
Inputstream in = demo. Class. getclassloader (). getresourceasstream ("DB. properties ");
Try {
Prop. Load (in );
} Catch (ioexception e ){
// Like the database configuration file, the entire program will not run if it is not loaded to the configuration file, so the exception here will be converted to an initialization error exception in actual development.
Throw new exceptionininitializererror (E );
}
}
}

/*************************************** * *** Second method ********************************* ******************************/

Class demo2 {
// The advantage of this method is that when someone changes the configuration file information, I access it again and get the changed information.
Private Static Properties prop = new properties ();
Static {
Try {
// Obtain the resource first
URL url = demo2.class. getclassloader (). getresource ("DB. properties ");
// Obtain the resource path
String Path = URL. getpath ();
// Obtain the stream related to the Resource
Fileinputstream in = new fileinputstream (PATH );
Prop. Load (in );
} Catch (ioexception e ){
// Like the database configuration file, the entire program will not run if it is not loaded to the configuration file, so the exception here will be converted to an initialization error exception in actual development.
Throw new exceptionininitializererror (E );
}
}
}

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.