JAVA concerning access to resources path

JAVA concerning access to resources path


1. Based on the file system is relatively simple:
       such a configuration File f = new File ( 'text.txt ');
       transferred here to the File constructor can be relative, such as a relative path is text.txt,
       also can be absolute, such as : new File ( 'C: /text.txt ');
       It should be noted that this relative path, the opposite of System.getProperties ( "user.dir") of the
       example, you use the cmd window by calling the java command to be execute a java program, then cmd in the current path, it is a relative path in Java programs
       , such as C: \> java test this command is executed, the relative path in the C drive.
   
2. based on the classpath:
       we know, java command implementation of the time you can specify a classpath, the system default classpath in the directory
       to find all kinds of calss following file, jar package and configuration files.
   
       there are three ways to get resources based on classpath:
       . this.getClass the uRL of url = () getResource ( "resource_name");
       the uRL of url = this.getClass () getClassLoader () getResource ( "resource_name");..
       the uRL of url =. Thread.currentThread () getContextClassLoader () getResource ( "resource_name"). ;
  
       . The first is an instance of class Class getResource method, the latter two methods are getResource ClassLoader class instance
       Class.getResource () is commissioned by the ClassLoader getResource method to achieve.
       so, to say getResource ClassLoader's methods: 
       (1) getResource method parameters ClassLoader can not be a "/" at the beginning, and they must start looking at the root, where the root directory is in the classpath directory and contains . cited jar
       such as the default of the eclipse classpath runtime Java classes each project is set to: 
       project root directory / bin directory and all the jar packages referenced in the project.
       at compile time, copy the src directory structure to the bin directory after, the java class compiled into class files along with the other files in the directory structure src copied to the original bin directory.
       suppose a plant classpath follows (2):
       / bin
       log4j-1.2.16.jar
       wherein log4j-1.2 there .16.jar directory structure org \ apache \ log4j \ corresponding to (the package org.apach.log4j)
       So find test.txt file in the bin directory, use the following method:
       ClassLoader.getResource ( "test.txt");
       Note that ClassLoader.getResource approach to the Senate must start looking at the root, where the root directory is in the classpath / bin.
       find bin / level1 / level2 / ll.txt files must ClassLoader.getResource ( "level1 / level2 / ll.txt "); // Note lookup must be based on the root directory (/ bin), and also to write the directory structure pair can not / at the beginning of
       (2) Class.getResource () is slightly different: 
           (a) can be found by a relative path, relative to the current instance of the Class document package resides;
           (B) may be, and the ClassLoader.getResource () Like to start looking at the root (classpath), but this time passing parameters to Class.getResource () must use the "/" beginning, otherwise, it is relatively searched (case (a) in the)
        fact, this Code is to / removed, and then calls the ClassLoader.getResource ()
        reference code:

public java.net.URL getResource(String name) {
                name = resolveName(name);
                ClassLoader cl = getClassLoader0();
                if (cl==null) {
                    // A system class.
                    return ClassLoader.getSystemResource(name);
                }
                return cl.getResource(name);
        }
            
        private String resolveName(String name) {
            if (name == null) {
                return name;
            }
            if (!name.startsWith("/")) {
                Class c = this;
                while (c.isArray()) {
                    c = c.getComponentType();
                }
                String baseName = c.getName();
                int index = baseName.lastIndexOf('.');
                if (index != -1) {
                    name = baseName.substring(0, index).replace('.', '/')
                        +"/"+name;
                }
            } else {
                name = name.substring(1);
            }
            return name;
        }


    It should be noted that this case based on the classpath looking for, before writing the code needed to study various systems classpath clear.
    For example, different versions of the Tomcat classpath settings are different, need to understand clearly.
    About getClassLoader () getResource and Thread.currentThread () getContextClassLoader () getResource difference. :
    Because Tomcat similar such containers may use the custom ClassLoader had a special classpath, so you need to follow a special way,
    the Thread .currentThread (). getContextClassLoader () returns the thread context ClassLoader, then call some of the insurance getResource more generally recommended Thread.currentThread (). getContextClassLoader (). getResource way to obtain resources.


3. Other acquisition path:
    1. Obtain current class where the "Project name Path":
        String = rootPath System.getProperty ( "user.dir");
        System.out.println (System.getProperty ( "user.dir")) ; // user.dir specified current path 
    
    2. obtain the compiled file "jar package path" (reflection):
        System.out.println (the System.getProperty ( "the java.class.path"));
    
    3. in a Servlet path made: 
        (1) to get the project directory:.. request.getSession () getServletContext ( ) getRealPath ( "") parameters specific to the package name. 
        Results: E: / the Tomcat / the webapps / Learning 
        (2) to give IE address bar Address: request.getRequestURL () 
        Results: HTTP: // localhost: 8080 / Learning / Learning 
        (. 3) obtained relative address: request.getRequestURI () 
        Results : / learning / learning 
    
    4.jsp acquired path: 
        to project called learning as an example: 
        (1) get the current page contains the full path name of the project: request.getRequestURI () 
        result: /learning/learning.jsp 
        (2) to get the project name: request.getContextPath () 
        result: / Learning 
        (3) get the current page directory full name: request.getServletPath () 
        result: If the page /learning/jsp/learning.jsp in jsp directory 
        (4) to get the full path of the server where the page: application.getRealPath ( "page .jsp") 
        results: D: /resin/webapps/learning/learning.jsp 
        (. 5) where the page server to obtain the absolute path: absPath = new java.io.File (application.getRealPath (  request.getRequestURI ())) getParent ();.
        results: D: / resin / webapps / learning

Published 38 original articles · won praise 7 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_43285577/article/details/97893855