Classloader. getresourceasstream (name); method for obtaining the configuration file

Source: Internet
Author: User
Classloader. getresourceasstream (name); Path ProblemInputstream in = getclass (). getresourceasstream ('/' + "spring-beans.dtd"); indicates to find the file under the classs directory, the file can be placed under SRC.
Inputstream in = getclass (). getresourceasstream ("spring-beans.dtd"); indicates finding a file from the path under the current classs
If it is in the class com. A. B. C. D. Test, it indicates that the spring-beans.dtd should be placed under the directory SRC \ com \ A \ B \ c \ D.

Saxbuilder builder = new saxbuilder ();
Entityresolver resolver = new entityresolver (){

Public inputsource resolveentity (string publicid,
String systemid ){
If (publicid. Equals ("-// spring // DTD bean // en ")){
Inputstream in = getclass (). getresourceasstream (
'/' + "Spring-beans.dtd ");
Return new inputsource (in );
} Else {
Return NULL;
}
}

};
Builder. setentityresolver (Resolver); (2) Example package ccutse. dB;
Import java. util .*;
Import java.net .*;
Import java. Io .*;

Public class readprop {
Private string user, psw, URL, driver;
Private int maxcon, mincon;
Public readprop (){

}


Public static void main (string [] ARGs ){
Readprop = new readprop ();
Inputstream in = NULL;
Try {
In = readprop. getclass (). getresourceasstream ("C: \ dB. properties"); the path should be placed under classpath.
If (in = NULL)
System. Out. println ("in is null ");
}
Properties prop = new properties ();
Try {
Prop. Load (in );
}
Catch (exception e ){
System. Out. println ("loading failed ");

}


}
} Class. getresourceasstream usage

First, getresourceasstream in Java has the following classes. 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 ");

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.