Usage of getResourceAsStream in Java

Source: Internet
Author: User

First, there are several getresourceasstream in Java:
1. Class.getresourceasstream (String Path): path does not start with '/' by default, the resource is fetched from the package where the class is located, and the '/' begins with the classpath root. It only constructs an absolute path through path, and eventually it gets the resources from the ClassLoader.

2. Class.getClassLoader.getResourceAsStream (String path): The default is to get from the classpath root (which is the classes folder that your project automatically generates), path cannot start with '/', Ultimately, resources are obtained by ClassLoader. (Has been verified)

3. ServletContext. getResourceAsStream (String path): The default is to fetch resources from the WebApp root directory, and whether or not Tomcat starts with '/' does not matter, 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.

Second, getResourceAsStream usage is roughly the following:

First: The file to be loaded and the. class file are in the same directory, for example: Com.x.y has a class me.class, and a resource file Myfile.xml

Then, you should have the following code:

Me.class.getResourceAsStream ("Myfile.xml");

Second: In the subdirectory of the Me.class directory, for example: Com.x.y under the class Me.class, while the Com.x.y.file directory has a resource file Myfile.xml

Then, you should have the following code:

Me.class.getResourceAsStream ("File/myfile.xml");

Third: Not under the Me.class directory, also not under the subdirectory, for example: Com.x.y under the class Me.class, while in the Com.x.file directory has a resource file Myfile.xml

Then, you should have the following code:

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

To summarize, maybe it's just two ways of writing

First: There is "/" in front

"/" represents the root of the project, such as the project name MyProject, "/" represents the MyProject

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

Second: There is no "/" in front

Directory representing the current class

Me.class.getResourceAsStream ("Myfile.xml");

Me.class.getResourceAsStream ("File/myfile.xml");

The source code is as follows:

Package Com.wpy.json;
public class Readpropertiesutil {public static void ReadProperties () {Properties prop = new properties ();//Create Properties Object InputStream in = ReadPropertiesUtil.class.getClassLoader (). getResourceAsStream ("Com/wpy/json/data.properties") ///load file to cache, start reading InputStream in = ReadPropertiesUtil.class.getResourceAsStream ("Data.properties") from classpath root;// Read the file of the current directory of the Readpropertiesutil class, Data.properties and Readpropertiesutil class in the same directory try {prop.load (in);//Load File Enumeration ENU = Prop.propertynames ();//Remove All Keywhile (Enu.hasmoreelements ()) {string key = (String) enu.nextelement (); System.out.println ("key=" +key+ ", value=" +prop.getproperty (key));}} catch (IOException e) {e.printstacktrace ();}}}


Reference from: http://blog.sina.com.cn/s/blog_72c8c1150100pw3r.html

Usage of getResourceAsStream in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.