Java的getResourceAsStream

The principle is related to the
class loader. Please refer to the principle of the class loader: http://www.cnblogs.com/xing901022/p/4574961.html
JDK description:
Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream(java.lang.String).


First, There are the following types of getResourceAsStream in Java:
1. Class.getResourceAsStream(String path): When the path does not start with '/', the default is to fetch resources from the package where this class is located, and when the path starts with '/', it is obtained from the root of the ClassPath . It just constructs an absolute path through path, and finally obtains resources from ClassLoader.

2. Class.getClassLoader.getResourceAsStream(String path) : By default, it is obtained from the root of ClassPath. The path cannot start with '/', and finally the resource is obtained by ClassLoader.

3. ServletContext.getResourceAsStream(String path): By default, resources are taken from the WebAPP root directory. It doesn't matter whether the path under Tomcat starts with '/', of course, this is related to the specific container implementation.

4. The application built-in object under Jsp is an implementation of the above ServletContext.

Secondly, the usage of getResourceAsStream is roughly as follows:

First: the file to be loaded and the .class file are in the same directory, for example: there is a class me.class under com.xy, and a resource file myfile.xml

, then, there should be as follows Code:

me.class.getResourceAsStream("myfile.xml");

Second: In the subdirectory of the me.class directory, for example: there is a class me.class under com.xy, and there are resource files in the com.xyfile directory myfile.xml

Then, there should be the following code:

me.class.getResourceAsStream("file/myfile.xml");

Third: not in the me.class directory, nor in subdirectories, for example: com.xy has class me .class , and there is a resource file myfile.xml in the com.x.file directory,

then, there should be the following code:

me.class.getResourceAsStream("/com/x/file/myfile.xml");

To sum up, it may just be Two ways of writing

First : there is " / " in front

"/" represents the root directory of the project, for example, the project name is myproject, "/" represents myproject

me.class.getResourceAsStream("/com/x/file/myfile.xml");

Second: there is no "/" before

Represents the directory of the current class

me.class.getResourceAsStream("myfile.xml");

me.class.getResourceAsStream("file/myfile.xml");

Finally, my own understanding:
The file path read by getResourceAsStream is only limited to the project In the source folder, including in the project src root directory, and anywhere in the class package, but if the configuration file path is in a folder other than the source folder, this method does not work.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326995736&siteId=291194637