Use the class loader to load the configuration file

Source: Internet
Author: User

 

Package advancejava; import static Java. lang. system. out; import Java. io. inputstream; import Java. lang. reflect. constructor; import Java. util. arraylist; import Java. util. collection; import Java. util. properties; public class propertiesload {/** U ** there are two ways to use the class loader to load the configuration file: 1. use the application class loader to load the configuration file. The loader starts searching for the path defined by Path 2. use * getresourceasstream of the current class to load the file. The loaded path is to search for */public static void main (string ARGs []) thro from the path of the current class. WS exception {// load ("advancejava/config. properties "); // load (" config. properties ");} public static void load (string properpath) throws exception {// inputstream in = // propertiesload. class. getclassloader (). getresourceasstream (properpath); // relative to the path inputstream in = propertiesload. class. getresourceasstream (properpath); // The path relative to the current class // find properties property = new properties (); property. load (in); s Tring classname = (string) property. get ("classname"); reflect (classname); In. close ();}/** instantiate an object using the obtained string class name, which relies on reflection. Can we instantiate the object accurately? */Public static void reflect (string classname) throws exception {collection Col = (Collection) class. forname (classname ). newinstance (); Col. add (1); Col. add (2); Col. add (3); out. println (Col. size ());}}

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.