Today, a reader replied to a question in my Android mobile phone number attribution query article by calling WebService. My sop12.xml file is stored in the src directory, not specific operation class addressservice. java is put in the same directory, causing misunderstanding.
- Inputstream = addressservice. Class. getclassloader ()
- . Getresourceasstream ("sop12.xml ");
Here we will summarize the usage of getresourceasstream in Java.
First, getresourceasstream in Java has the following types:
1. Class. getresourceasstream (string path): When the path does not start with '/', resources are retrieved from the package where the class is located by default.
Obtained under the classpath root. It only constructs an absolute path through path, and finally gets resources by classloader.
2. Class. getclassloader. getresourceasstream (string path)
Classloader Obtains resources.
3. servletcontext. getresourceasstream (string path): resources are retrieved from the webapp root directory by default. It doesn't matter if the path in Tomcat starts,
Of course, this is related to the specific container implementation.
4. The application built-in object in JSP is an implementation of the above servletcontext.
Secondly, getresourceasstream has the following usage:
First, the file to be loaded is in the same directory as the. Class file. For example, there is a class me. Class under com. x. y and a resource file myfile. xml.
The following code should be available:
Me. Class. getresourceasstream ("myfile. xml ");
Second: in me. sub-directories in the class directory, such as COM. x. Y has a class me. class, at the same time in COM. x. y. the file directory contains the resource file myfile. XML
The following code should be available:
Me. Class. getresourceasstream ("file/myfile. xml ");
Third: Not me. the class directory is not in the subdirectory, for example: COM. x. Y has a class me. class, at the same time in COM. x. the file directory contains the resource file myfile. XML
The following code should be available:
Me. Class. getresourceasstream ("/COM/X/file/myfile. xml ");
To sum up, there may be only two ways to write
First, there is a forward slash (/).
"/" Indicates the root directory of the project. For example, the project name is myproject, and "/" indicates myproject.
Me. Class. getresourceasstream ("/COM/X/file/myfile. xml ");
Second, there is no "/"
Indicates the directory of the current class
Me. Class. getresourceasstream ("myfile. xml ");
Me. Class. getresourceasstream ("file/myfile. xml ");
Finally, I understand:
The file path read by getresourceasstream is limited to the source folder of the project, including the SRC root directory of the project and any location in the class package, however, if the path of the configuration file is in a folder other than the source folder, this method cannot be used.
Note: Java. Lang. classloader. gclassloader () source code
/** * Returns an input stream for reading the specified resource. * * <p> The search order is described in the documentation for {@link * #getResource(String)}. </p> * * @param name * The resource name * * @return An input stream for reading the resource, or <tt>null</tt> * if the resource could not be found * * @since 1.1 */ public InputStream getResourceAsStream(String name) {URL url = getResource(name);try { return url != null ? url.openStream() : null;} catch (IOException e) { return null;} }
Java. Lang. classloader. getresource () source code
/** * Finds the resource with the given name. A resource is some data * (images, audio, text, etc) that can be accessed by class code in a way * that is independent of the location of the code. * * <p> The name of a resource is a '<tt>/</tt>'-separated path name that * identifies the resource. * * <p> This method will first search the parent class loader for the * resource; if the parent is <tt>null</tt> the path of the class loader * built-in to the virtual machine is searched. That failing, this method * will invoke {@link #findResource(String)} to find the resource. </p> * * @param name * The resource name * * @return A <tt>URL</tt> object for reading the resource, or * <tt>null</tt> if the resource could not be found or the invoker * doesn't have adequate privileges to get the resource. * * @since 1.1 */ public URL getResource(String name) {URL url;if (parent != null) { url = parent.getResource(name);} else { url = getBootstrapResource(name);}if (url == null) { url = findResource(name);}return url; }
Java. Lang. Class. getresourceasstream (string name) source code
/** * Finds a resource with a given name. The rules for searching resources * associated with a given class are implemented by the defining * {@linkplain ClassLoader 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 {@link * ClassLoader#getSystemResourceAsStream}. * * <p> Before delegation, an absolute resource name is constructed from the * given resource name using this algorithm: * * <ul> * * <li> If the <tt>name</tt> begins with a <tt>'/'</tt> * (<tt>'\u002f'</tt>), then the absolute name of the resource is the * portion of the <tt>name</tt> following the <tt>'/'</tt>. * * <li> Otherwise, the absolute name is of the following form: * * <blockquote><pre> * <tt>modified_package_name</tt>/<tt>name</tt> * </pre></blockquote> * * <p> Where the <tt>modified_package_name</tt> is the package name of this * object with <tt>'/'</tt> substituted for <tt>'.'</tt> * (<tt>'\u002e'</tt>). * * </ul> * * @param name name of the desired resource * @return A {@link java.io.InputStream} object or <tt>null</tt> if * no resource with this name is found * @throws NullPointerException If <tt>name</tt> is <tt>null</tt> * @since JDK1.1 */ public InputStream getResourceAsStream(String name) { name = resolveName(name); ClassLoader cl = getClassLoader0(); if (cl==null) { // A system class. return ClassLoader.getSystemResourceAsStream(name); } return cl.getResourceAsStream(name); }
Java. Lang. Class. getresource (string
Name) source code
/** * Finds a resource with a given name. The rules for searching resources * associated with a given class are implemented by the defining * {@linkplain ClassLoader 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 {@link * ClassLoader#getSystemResource}. * * <p> Before delegation, an absolute resource name is constructed from the * given resource name using this algorithm: * * <ul> * * <li> If the <tt>name</tt> begins with a <tt>'/'</tt> * (<tt>'\u002f'</tt>), then the absolute name of the resource is the * portion of the <tt>name</tt> following the <tt>'/'</tt>. * * <li> Otherwise, the absolute name is of the following form: * * <blockquote><pre> * <tt>modified_package_name</tt>/<tt>name</tt> * </pre></blockquote> * * <p> Where the <tt>modified_package_name</tt> is the package name of this * object with <tt>'/'</tt> substituted for <tt>'.'</tt> * (<tt>'\u002e'</tt>). * * </ul> * * @param name name of the desired resource * @return A {@link java.net.URL} object or <tt>null</tt> if no * resource with this name is found * @since JDK1.1 */ public java.net.URL getResource(String name) { name = resolveName(name); ClassLoader cl = getClassLoader0(); if (cl==null) { // A system class. return ClassLoader.getSystemResource(name); } return cl.getResource(name); }
We can see that the class. getresource () and Class. getresourceasstream () Methods
The rule for searching resources related to a given class is implemented by defining the class loader of the class. This method delegates the class loader of this object. If this object is loaded through the bootstrap loader, this method is delegated ClassLoader.getSystemResourceAsStream(java.lang.String). And ClassLoader.getSystemResource(java.lang.String).
Before delegation, use the following algorithm to construct an absolute resource name from the given Resource Name:
========================================================== ========================================================== ============================
Author: Ouyang Peng: Welcome to repost. sharing with others is the source of progress!
Reprinted Please retain the original address: http://blog.csdn.net/ouyang_peng
========================================================== ========================================================== ============================