Classpath get--getresource ()

Source: Internet
Author: User

In the Java API, there are two ways to read resources using CLASSPATH.

1. Class GetResource ()

2. ClassLoader's GetResource ()

But there are some differences between the two, run the following program:

 PackageZero.xml.config; Public classMain { Public Static voidMain (string[] args) {NewMain (). Testgetresource (); }         Public voidTestgetresource () {System.out.println (Main).class. GetResource ("/"). GetPath ()); System.out.println (Main.class. GetResource ("/app.properties"). GetPath ()); System.out.println (Main.class. GetResource (""). GetPath ()); System.out.println (Main.class. GetResource ("App.properties"). GetPath ()); System.out.println ("-------------------"); System.out.println ( This. GetClass (). GetResource ("/"). GetPath ()); System.out.println ( This. GetClass (). GetResource ("/app.properties"). GetPath ()); System.out.println ( This. GetClass (). GetResource (""). GetPath ()); System.out.println ( This. GetClass (). GetResource ("App.properties"). GetPath ()); System.out.println ("-------------------"); System.out.println (Main.class. getClassLoader (). GetResource (""). GetPath ()); System.out.println (Main.class. getClassLoader (). GetResource ("App.properties"). GetPath ()); System.out.println (Main.class. getClassLoader (). GetResource ("Zero/xml/config"). GetPath ()); System.out.println (Main.class. getClassLoader (). GetResource ("Zero/xml/config/app.properties"). GetPath ()); }}

The resulting output is:

/home/rain/git/spring-self-learn/bin//home/rain/git/spring-self-learn/bin/app.properties/home/rain/ git/spring-self-learn/bin/zero/xml/config//home/rain/git/spring-self-learn/bin/zero/xml/config/  App.properties-------------------/home/rain/git/spring-self-learn/bin//home/rain/git/spring-self-learn/ bin/app.properties/home/rain/git/spring-self-learn/bin/zero/xml/config//home/rain/git/ spring-self-learn/bin/zero/xml/config/app.properties-------------------/home/rain/git/ spring-self-learn/bin//home/rain/git/spring-self-learn/bin/app.properties/home/rain/git/ spring-self-learn/bin/zero/xml/config/home/rain/git/spring-self-learn/bin/zero/xml/config/ App.properties

That is

1. If you want to obtain classpath, use the following methods:

System.out.println (Main.  Class. GetResource ("/"). GetPath ()); System.out.println (Main. class. getClassLoader (). GetResource (""). GetPath ());

2. If you want to obtain the files under Classpath, use the following methods:

System.out.println (Main.  Class. GetResource ("/app.properties"). GetPath ()); System.out.println (Main. class. getClassLoader (). GetResource ("App.properties"). GetPath ());

3. If you want to obtain the path to the current class (such as Zero.xml.config.Main), use the following methods:

System.out.println (Main.  Class. GetResource (""). GetPath ()); System.out.println (Main. class. getClassLoader (). GetResource ("Zero/xml/config"). GetPath ());

4. If you want to obtain a file under the current classpath, use the following method:

System.out.println (Main.  Class. GetResource ("App.properties"). GetPath ()); System.out.println (Main. class. getClassLoader (). GetResource ("Zero/xml/config/app.properties"). GetPath ());

Note that if the obtained file or path does not exist, getresource () returns NULL. For example, getClassLoader (). GetResource ("/") will return null.

Classpath get--getresource ()

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.