Differences between getclassloader (). getresourceasstream (filename), Class. getresourceasstream (filename), and (). getcontextclassloader (). getresourceasstream (filename)

Source: Internet
Author: User

I. Preface

When I got the property myself, I met xx. class. getresourceasstream (filename), I don't really understand this. I checked the information online and saw some of the above, So I studied it.

 

Ii. Content

Let's talk about the differences.

Getclassloader (). getresourceasstream ("a.txt"): the path of this method, whether it is an absolute path or a correct path, is obtained from the path, even if ". /a.txt "form to look for, here we need to pay attention to the null pointing to the exception of the situation (you can look at the write here http://cache.baiducontent.com/cm=9d78d513d99c12eb0fb1837e7c4380200e55f0326284915468d5e316ce370d160771e2cb30536713a0b66b6671f30e02b4e47132690c7af1dd8a9f4baea68f7871d57223706bdd124d9b58e5dc46529e778d1bb3f25cf0ba8768d5f18cc4de20089c44040c84f3895803&p=9274c54ad5c246e74bbe9b7c4605bb&ne WP = users & User = Baidu & fm = SC & query = Maven + getclassloader % Ce % aanull & qid = bf473e2d0001b6a7 & p1 = 3 ). Write the bootstrap classloader (start class loader or Bootstrap Class Loader) to load the JDK core APIs. These APIs are generally located under jdk_home/lib. It is a local interface, therefore, it cannot be obtained from Java code. For example, what log (Java. Lang. String. Class. getclassloader () gets is null (reference from the http://blog.csdn.net/benjieming_wang/article/details/5623492 ). Classloader is a type of loader.

Now I will attach the Code Description:

Package Org. wh. properties; import Java. io. ioexception; import Java. io. inputstream; import Java. util. iterator; import Java. util. properties; import Java. util. set; public class propertiesdemo01 {properties prop; Public void setprop (properties prop) {This. prop = prop;} public static void main (string ARGs []) {loadconfig ();} public static properties loadconfig () {properties prop = new properties (); // The getresource () method of the class is from the current. the class file path searches for resources, while the classloader searches for resources from the jar package // inputstream input = properties. class. getresourceasstream ("/jdbcutils. properties "); inputstream input1 = propertiesdemo01.class. getclassloader (). getresourceasstream ("jdbcutils. properties "); system. out. println ("JDK classloader" + properties. class. getclassloader (); // result: JDK Class Loader: NULL system. out. println ("Demo:" + propertiesdemo01.class. getclassloader (); // result: Demo: [email protected] Try {prop. load (input1); @ suppresswarnings ("rawtypes") set keyValue = prop. keyset (); For (iterator A = keyValue. iterator ();. hasnext ();) {string key = (string). next (); string value = prop. getproperty (key); system. out. println ("-key value --" + key + "Value -- Value-" + value) ;}} catch (ioexception e) {e. printstacktrace () ;}return prop ;}}
Conclusion: if the internal JDK class is used here, null pointing to an exception will be reported. We can see that the result of properties. Class. getclassloader () is null;

Class. getresourceasstream (filename): If "/" is added to filename, it indicates the absolute path. Otherwise, it indicates the relative path. The code can be seen above
Thread. currentthread (). getcontextclassloader (): This is the class loader of the current thread,

Package Org. wh. properties; public class test {public static void main (string [] ARGs) {system. out. println (thread. currentthread (). getcontextclassloader (); // The class loader System of the current thread. out. println (test. class. getclassloader (); // The Class Loader of the current class system. out. println (classloader. getsystemclassloader (); // The initial class loader }}

See here for reference: http://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream
  


Differences between getclassloader (). getresourceasstream (filename), Class. getresourceasstream (filename), and (). getcontextclassloader (). getresourceasstream (filename)

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.