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 ()