GetResourceAsStream usage in Java

Source: Internet
Author: User

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. Resources starting with '/' are obtained from the ClassPath root. It only constructs an absolute path through path, and finally gets resources by ClassLoader. 2. Class. getClassLoader. getResourceAsStream (String path): It is obtained from the root of the ClassPath by default. The path cannot start with '/' and is finally obtained by the ClassLoader. 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 with '/'. 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 files to be loaded and. the class file is in the same directory, for example, com. x. y has a class me. class with the resource file myfile. xml, the following code should be available: me. class. getResourceAsStream ("myfile. xml "); 2: 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. CIA Ss. getResourceAsStream ("/com/x/file/myfile. xml "); To sum up, there may be only two ways to write first:"/"and"/"in front represent the root directory of the project. For example, the project name is myproject, "/" represents myproject me. class. getResourceAsStream ("/com/x/file/myfile. xml "); Second: No"/"above represents the directory me of the current class. class. getResourceAsStream ("myfile. xml "); me. class. getResourceAsStream ("file/myfile. xml "); finally, you can understand that the file path read by getResourceAsStream is limited to the source folder of the project, including under the src root directory of the project and any location in the class package, however, if the configuration file path is in a folder other than the source folder This method cannot be used.

Related Article

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.