The example of this article tells the usage of getResourceASSTREAM in Java. Share it for everyone for your reference. The specifics are as follows:
(1) GetresourceASSTREAM in Java has the following situations:
1. Class.getResourceasstream (String Path):
#Path does not take resources by default at the beginning of '/' at the beginning;
#Is obtained from the ClassPath root. The principle is to construct an absolute path through PATH, and eventually obtain resources by ClassLoader.
2. Class.GetClassloader.getResourceASSTRAM (String Path):
#The default is obtained from the ClassPath root. Path cannot start with '/', and eventually obtains resources by ClassLoader.
3. ServletContext. GetresourceASSTRAM (String Path):
#From the webapp root directory, whether the path under TOMCAT or Resin#[Web container] does not matter whether the path starts with '/', of course, this is also related to the specific container.
4. Applicative under the JSP is a implementation of the serviceTContext above.
(2) Among them, GetRSOURCEASSTREAM has the following uses:
First: The files to be loaded and the .class files in the same directory, for example: there are ME.Class under the com.xy, and resource file myfile.xml
Then, there should be the following code:
Copy code code as follows: me.class.getResourceASSTREAM ("MyFile.xml");
Second: In the sub -directory of the Me.Class directory, for example: there is a class ME.Class under the com.xy, and there is a resource file myfile.xml in the com.xyfile directory
Then, there should be the following code:
Copy code code as follows: me.class.getResourceASSTREAM ("File/MyFile.xml");
Third: not in the Me.Class directory, nor is it in the subdirectory, for example: there is a class me.class under the com.xy, and there is a resource file myfile.xml in the com.x.file directory.
Then, there should be the following code:
Copy code code as follows: me.Class.getResourceASSTREAM ("/COM/X/FILE/MyFile.xml");
(3) The summary is as follows, it may be just two ways of writing
First: " /" in front
" /" Represents the root directory of the project, such as the project name is MyProject, and " /" represents myproject
Copy code code as follows: me.Class.getResourceASSTREAM ("/COM/X/FILE/MyFile.xml");
Second: There is no " /" in front
The dictation code code for the current class is as follows: me.class.getResourceasstream ("myfile.xml");
me.class.getResourceasstream ("file/myfile.xml");
It is hoped that this article is helpful to everyone's Java program design.