Java to take resources, often used class.getresource and classloader.getresource, here to see their resource files when the path problem.
Class.getResource (String path)
When path does not start with '/', the default is to take resources from the package where the class resides;
When path begins with '/', it is obtained from the classpath root;
What does that mean? Look at the output of the following code to see:
Package testpackage;
public class Testmain {
public static void Main (string[] args) {
System.out.println (TestMain.class.getResource (""));
System.out.println (TestMain.class.getResource ("/"));
}
}
Output results:
file:/e:/workspace/test/bin/testpackage/
file:/e:/workspace/test/bin/
As mentioned above, "path begins with '/' and is fetched from the classpath root," which is equivalent to the bin directory (under the Eclipse environment).
Let's take another example, assuming the following project structure:
O_packagestructure1o_packagestructure1
How do we write a path if we want to take the 1~3.properties file separately in the Testmain.java? The code is as follows:
Package testpackage;
public class Testmain {
public static void Main (string[] args) {
The package directory where the current class (class) resides
System.out.println (TestMain.class.getResource (""));
Class Path root directory
System.out.println (TestMain.class.getResource ("/"));
Testmain.class in the <bin>/testpackage bag.
2.properties in <bin>/testpackage Package
System.out.println (TestMain.class.getResource ("2.properties"));
Testmain.class in the <bin>/testpackage bag.
3.properties in <bin>/testpackage.subpackage Package
System.out.println (TestMain.class.getResource ("subpackage/3.properties"));
Testmain.class in the <bin>/testpackage bag.
1.properties in Bin directory (class root directory)
System.out.println (TestMain.class.getResource ("/1.properties"));
}
}
※class.getresource and Class.getresourceasstream in use, the path selection is the same.
Class.getclassloader (). GetResource (String path)
Path cannot begin with '/';
The path is obtained from the classpath root;
Let's take a look at the output of the following piece of code:
Package testpackage;
public class Testmain {
public static void Main (string[] args) {
Testmain t = new Testmain ();
System.out.println (T.getclass ());
System.out.println (T.getclass (). getClassLoader ());
System.out.println (T.getclass (). getClassLoader (). GetResource (""));
System.out.println (T.getclass (). getClassLoader (). GetResource ("/"));//null
}
}
Output results:
Class Testpackage. Testmain
Sun.misc.launcher$appclassloader@1fb8ee3
file:/e:/workspace/test/bin/
Null
From the result "TestMain.class.getResource ("/") = = T.getclass (). getClassLoader (). GetResource (" ")"
If you have the same project structure
O_packagestructureo_packagestructure
uses Class.getclassloader (). GetResource (String path) to write this:
Package testpackage;
public class Testmain {
public static void Main (string[] args) {
Testmain t = new Testmain ();
System.out.println (T.getclass (). getClassLoader (). GetResource (""));
System.out.println (T.getclass (). getClassLoader (). GetResource ("1.properties"));
System.out.println (T.getclass (). getClassLoader (). GetResource ("testpackage/2.properties"));
System.out.println (T.getclass (). getClassLoader (). GetResource ("TESTPACKAGE/SUBPACKAGE/3. Properties "));
}
}
※ Class.getclassloader (). GetResource and Class.getclassloader (). getResourceAsStream in use, the path selection is the same.