The difference between absolute path, relative path, root path and application in drive letter, Web (detailed, attached: path problem of getClassLoader (). GetResource () and getResource ())

For a detailed explanation of the path problem, please be patient!


Path classification: absolute path , relative path and root path


Absolute path: Absolute path refers to the absolute position under the directory, directly to the target position, usually starting from the drive letter. For example: the path of the host file we are familiar with C:\windows\system32\host
is the absolute path in the Web: used to jump between different websites, such as:http://www.baidu.com.image/sky.png


Relative path: Pair path refers to the path relationship with other files (or folders) caused by the path where this file is located. Using relative paths can bring us a lot of convenience. For example: the same is the example of the path of the host file. If the current path is C:\windows, use a relative path to indicate the path of the host file: system32\host(omit " . ")
Relative path in the Web : used in the same website, such as:, image/1.jpg only for static resources , If there are more pages and frames are used, there will be confusion


" . " Indicates the current path, which can be omitted in the case of a channel, only in special cases. For example: it .system32\host is the same as the relative path of the host file path!
".." Represents the parent directory, for example: suppose our current path is the path c:\program filesto find the host file, we need to enter it ..\windows\system32\host. If we c:\program files\Windows appsstill want to find the path of the host file in the directory, we need to enter..\..\windows\system32\host


Root path: The root path is also called the root directory. The root directory refers to the top-level directory of the logical drive, which is relative to the subdirectory. Open "My Computer", double-click the C drive to enter the root directory of the C drive, double-click the D drive to enter the root directory of the D drive. Other analogies. The root directory is created when the file system is established, and its purpose is to store the directory items of subdirectories (also called folders) or files. A "tree" directory tree, the root of the tree is its root (root directory). Assuming that apple.jpgthe picture named in the images folder under the D drive , the root directory can be written as img src="/images/apple.jpg"
the root path in the Web: the website root directory refers to a server on which multiple sites are placed simultaneously, or a large-scale site needs to be On several servers. "/" Means the root directory, for example: the host name (server) specified in Web /servletdemo/loginservlet, if in the browser, "/" means the host name http://localhost:8080/. /loginservlet, If in the server, "/" indicates the project path /servletdemo;
note: " .. " also applies to the root directory path


Understand the above path problem, then look at the path problem of getClassLoader (). GetResource () and getResource () is simple!

. getClass (). getResource (file): indicates that the file will only be found in the same path as the current calling class
. getClass (). getClassLoader (). getResource (file): indicates that it will only be in the root directory (/) Find file

It can be understood as follows: getClassLoader () means just a "/".

To illustrate with an example: we remove the getResource method and only observe the role of the getClassLoader method. When we use the flow that comes with the tool class to read the properties file in the JDBC phase of the Web, the path will be set, and then the way to set the path is the root path. (Assume that the properties file is created under src and the DBUtils tool class is created in the com.xxx.java.utils package)

The following two ways of configuration file path have the same effect and can be read! Do not believe you can try it!

Insert picture description here
Insert picture description here


If you feel satisfied, you can like and follow it! Keep sharing all kinds of knowledge ...

Published 155 original articles · praised 337 · 90,000 views +

Guess you like

Origin blog.csdn.net/weixin_44170221/article/details/105546946