Access to resources in Java files under the classpath

ClassLoader  provides two methods for obtaining resources from the loading class path:

      public URL  getResource (String name);  
      public InputStream  getResourceAsStream (String name);  


      Here name is the path of the resource type, and it is the relative position of the "/" in the root path. getResource get a URL object is to locate the resource, and the resource reference getResourceAsStream obtain assurance program input stream may extract data from the correct position.
      But not ClassLoader of these two methods actually used, but the Class of getResource and getResourceAsStream method because the Class object can be obtained from your class (such as YourClass.class or YourClass.getClass ()), and the need to call the ClassLoader once YourClass.getClassLoader () method, but according to the statement JDK documentation, these two methods Class object is actually a "commission" (delegate) to load its ClassLoader to do, so just use Class objects to these two methods it.

     Therefore, a direct call   this.getClass () getResourceAsStream (String name).  ; Acquiring flow,

               The method of using static ClassLoader.getSystemResourceAsStream (String name)  ;.

     Here are some methods to get classpath and the absolute path of the current class. You may need to use some of the methods you need to get the absolute path of the resource.

1. this.getClass (). GetResource ( "") 
to get the current directory is the URI class class files. Not including yourself!
Such as: File: / D: / Workspace / jbpmtest3 / bin / COM / Test /

2. this.getClass () the getResource ( "/"). 
The resulting current is the classpath absolute URI path  .
Such as: File: / D: / Workspace / jbpmtest3 / bin /

3. this.getClass ()  . GetClassLoader () getResource ( ""). 
Also the current ClassPath obtained an absolute URI path  .
Such as: File: / D: / Workspace / jbpmtest3 / bin /

4. ClassLoader.getSystemResource ( "") 
is obtained ClassPath current absolute URI path .
Such as: File: / D: / Workspace / jbpmtest3 / bin /

5. The Thread.currentThread () getContextClassLoader.  () .GetResource ( "") 
obtained is ClassPath the current absolute URI path  .
Such as: File: / D: / Workspace / jbpmtest3 / bin /

6. The ServletActionContext.getServletContext () the getRealPath ( "/"). 
Web application  , the root of the Web application to obtain the absolute path. In this way, we only need to provide a path relative to the Web application root directory, you can build an absolute path to locate resources.
Such as: file: / D: /workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/WebProject


Precautions:

1. Do not use with respect System.getProperty ( "user.dir" ) relative to the current user directory. This is a time bomb, it could kill you at any time.

2. Try to use the URI form of absolute path of the resource. It can be easily converted to URI, URL, File object.

3. Try to use a relative path opposite the classpath. Do not use an absolute path. Using the above ClassLoaderUtil class public static URL getExtendResource (String relativePath) method has been able to use the relative position of the classpath resources all positions.

4. Never use absolute paths hard-coded. Because we can use getResource ClassLoader class ( "") method to get the absolute path of the current classpath. If you must specify an absolute path, using a configuration file, much better than hard-coded!

The method of obtaining outside CLASSPATH path:
the URL this.getClass Base = () the getResource ( ""); // get the first class of the present position is located, such as / Home / Popeye / testjava / Build / classes / NET /.  
     String path = new File (base.getFile (), " ...... / ...... / ...... /" + name) .getCanonicalPath (); // can be obtained / home / popeye / testjava / name

Further, if the boot program from the ANT, the this .getClass (). getResource ( "" ) taken out of the more strange, JAVA command line directly with debugging you can be successful.

 

 

        // classpath目录下
        URL resource = TestASM.class.getResource("/");
        // classpath+该类所在路径
        URL resource1 = TestASM.class.getResource("");
        // classpath目录下
        URL resource2 = TestASM.class.getClassLoader().getResource("");
        // null
        URL resource3 = TestASM.class.getClassLoader().getResource("/");
         //file:/D:/sourceCode/demo4/target/classes/
        System.out.println(resource);
        //file:/D:/sourceCode/demo4/target/classes/com/example/demo4/asm/
        System.out.println(resource1); 
        //file:/D:/sourceCode/demo4/target/classes/
        System.out.println(resource2);
        //null
        System.out.println(resource3);

 

        // classpath下code.txt
        InputStream asStream = TestASM.class.getClassLoader().getResourceAsStream("code.txt");
        System.out.println(asStream);
        // 加 / 表示获取classpath下code.txt,不加/返回为null
        InputStream asStream1 = TestASM.class.getResourceAsStream("/code.txt");
        System.out.println(asStream1);
        // 获取classpath下code.txt文件
        InputStream asStream2 = TestASM.class.getClassLoader().getSystemResourceAsStream("code.txt");
        System.out.println(asStream2);
        System.out.println("--------------------------------------");
         // use tools ResourceUtils get the classpath file 
        File file1 = ResourceUtils.getFile ( "classpath: code.txt" ); 
        FileInputStream IS = new new FileInputStream (file1); 
        System.out.println (IS); 
        System.out. the println ( "--------- using the URL to open the object file stream, the following examples ---------" ); 
        the inputStream inputStream . = TestASM class .getResource ( "/ code.txt" ). openStream (); 
        a FileOutputStream fos = new new a FileOutputStream ( "C: \\ 18,030,501 the Users \\ \\ \\ aaa.txt Desktop" );
         byte [] = BYS new new  byte [1024];
        int len;
        while ((len = inputStream.read(bys)) != -1) {
            fos.write(bys, 0, len);
            fos.flush();
        }
        inputStream.close();
        fos.close();

 

Guess you like

Origin www.cnblogs.com/diandianquanquan/p/11492925.html