Ways to get paths in Java self-analysis

Source: Internet
Author: User

As far as I'm concerned, the two most common ways to get paths are

class.getrecource (filename) and class.getclassloader.getRecource (filename)

The difference between the two is very simple is the path is a bit different, here is mainly about two parameters, the other path to get, the other words in accordance with the relative path one by one to find the line

Class.getrecource (filename):

Parameter "/" Represents the Get root directory; (That is, the bin directory we used to store "bytecode file directory"
" " Represents getting the current class path

CLASS.GETCLASSLOADER.GETRECOURC (filename):

Parameter "/" Is null is worthwhile because the underlying is C + + written
“ “ is to get the root directory (that is, the bin directory)

Well, specifically how to achieve,

Let's look at an example.

How to get the 1.txt in this bin directory?

String Url=test6. class. GetResource ("/1.txt"). GetFile ();   URL1=test6. class. getClassLoader (). GetResource ("1.txt"). GetFile ();   Results:/f:/checkout/workspacefuxi/thread_1/bin/1.txt  
String Url=test6. class. GetResource (""). GetFile ();  CheckOut/workspacefuxi/thread_1/bin/test1/    

Then why does this happen?

Then let's analyze his source code, this is the source of Class.getrecource ():

 Public Java.net.URL getresource (String name) {         = resolvename (name);          = GetClassLoader0 ();          if (cl==null) {             //  A system class.             return Classloader.getsystemresource (name);         }          return Cl.getresource (name);     }  

Here it is clear to see that eventually he is going to execute Cl.getresource (name), which is tantamount to executing the class.getclassloader (). Getrecource () method

So, the two are actually consistent.

Why is there a difference in getting the path, let me take a look at the source code of ResolveName (name)

So you can clearly understand why both of these things are the case.

Privatestring ResolveName (string name) {if(Name = =NULL) {             returnname; }         if(!name.startswith ("/") {Class C= This;  while(C.isarray ()) {C=C.getcomponenttype (); } String baseName=C.getname (); intindex = Basename.lastindexof ('. ')); if(Index! =-1) {Name= basename.substring (0, index). Replace ('. ', '/'))                     +"/"+name; }         } Else{//Here it is clear that there is a '/' time to remove itName = name.substring (1); }         returnname; }  

Finally, I'd like to talk about the role of getClassLoader ():

This is a better explanation on the Internet:

Java is an object-oriented language, object-oriented language is the purpose of everything is the object, then the class is also an object, the properties and methods inside the class are also objects. What's inside Java

Some classes are objects of class, and this this.class is the object that gets the class relative to the class. The next method is to get the loader for the class object.

Only class has getClassLoader () method Ah ~ can think so, we usually talk about XXX class, but we do not say this class how to deal with the virtual machine, how the virtual machine recognize this

Classes. You can't rely on strings all the time. So Java has designed class. Used for virtual machine management of classes. When a class is loaded by a virtual machine, it will create a class

example, for Class A is A.class, and for Class B is b.class. Class classes also provide many ways to get information about a class. You know, the loader of a class is divided into "Start class loader", "with

User-defined loader ". It has more than one class class that needs to hold this information. getClassLoader () is used to obtain this information.

Ways to get paths in Java self-analysis

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.