Gets the specified resource in the project in Java getResourceAsStream usage by getResourceAsStream method

Gets the specified resource in the project by getResourceAsStream method

A: Gets the resources under src

    1), Class.getResourceAsStream (String path): path is not in '/' default from the package where such resources taken at the beginning, with '/' is from the beginning of the root ClassPath (i.e., '/' represents src) Get . It is only by constructing a path absolute path, and ultimately access to resources by the ClassLoader.

            Example:

                    A), in the same directory : under com.xy have class me.class, while the resource file myfile.xml in the same directory, you should use: me.class.getResourceAsStream ( "myfile.xml");

                    B), in a subdirectory : For example: In com.xy have class me.class, while the resource file myfile.xml in com.xyfile directory, you should use me.class.getResourceAsStream ( "file / myfile.xml" );

    2), Class.getClassLoader.getResourceAsStream (String path): The default is taken from the root ClassPath, path not begin with '/', is ultimately acquired by the resource ClassLoader.

            Example:

                   A), not under the same directory, nor in a subdirectory : For example: In com.xy have class me.class, while the resource file myfile.xml in com.x.file directory, you should use: me.class .getClassLoader.getResourceAsStream ( "com / x / file / myfile.xml");

Two: Gets the specified resource in the web project

    1), ServletContext getResourceAsStream. (String path): default (that is, from WebAPP root: root directory of the project to be published in the server (with the same level of web files under src folder)) to take the resource under, path whether '/ 'beginning does not matter;

           Example:

                A), there is at the root of the web project myfile.xml file, you should use:

getServleContext().getResourceAsStream("myfile.xml");

    2), application of the built-in objects is a kind of above Jsp ServletContext implementation.

 

Java usage in getResourceAsStream

First, Java is getResourceAsStream are the following: 
1. Class.getResourceAsStream (String path): When the path is not to begin '/' resource from the default is taken is located at such a package, with '/' is from the beginning of the root ClassPath under acquisition. It is only by constructing a path absolute path, and ultimately access to resources by the ClassLoader. 

2. Class.getClassLoader.getResourceAsStream (String path): The default is taken from the root ClassPath, path not begin with '/', is ultimately acquired by the resource ClassLoader. 

. 3. ServletContext getResourceAsStream (String path) : Default WebAPP taken from the root directory of resources, Tomcat whether the path leading '/' does not matter, of course, this particular container and is implementation dependent. 

Application in built-in objects is a kind of above 4. Jsp ServletContext implementation. 

Secondly, getResourceAsStream usage of the following categories: 

First: To load files and .class files in the same directory, for example: there are classes me.class under com.xy, while the resource file myfile.xml 

So, there should be as follows Code: 

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

second: me.class directory in a subdirectory, for example: class me.class there under com.xy, while the resource file in the directory com.xyfile myfile.xml 

then, you should have the following code: 

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

Third: not under me.class directory or in a subdirectory, for example: class me.class there under com.xy, while com.x.file there are a resource file directory myfile.xml 

so, there should be the following code: 

me.class.getResourceAsStream ( "/ COM / the X-/ file / myfile.xml"); 

sum up, it may be just two way 

first: preceded by "/ " 

" / "represents the root directory of the project, such as project name, called myproject," / "represents the myproject 

me.class.getResourceAsStream (" / COM / the X-/ File / myfile.xml "); 

second: not preceded by" / " 

represents the current directory class 

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

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

and finally, their own understanding:  
the file path getResourceAsStream read confined and Engineering source folder comprises root of src in the project, and any position inside the packet type, but if the configuration file in addition to other files when the source folder, the method is not take.

Guess you like

Origin www.cnblogs.com/Lee-yl/p/11459641.html