Method for obtaining the current path in Java
1. system. getproperty ("user. dir") Relative Path of the current user directory.
2. This. getclass (). getresource ("") is also the absolute URI path of the current classpath.
3. classloader. getsystemresource ("") is also the absolute URI path of the current classpath.
4. thread. currentthread (). getcontextclassloader (). getresource ("") is also the absolute URI path of the current classpath.
How to read the properties file in Java
1. Use the load () method of Java. util. properties class
Example: inputstream in = lnew bufferedinputstream (New fileinputstream (name ));
Properties P = new properties ();
P. Load (in );
2. Use the getbundle () method of the Java. util. resourcebundle class
Example: resourcebundle RB = resourcebundle. getbundle (name, locale. getdefault ());
3. Use the constructor of the Java. util. propertyresourcebundle class
Example: inputstream in = new bufferedinputstream (New fileinputstream (name ));
Resourcebundle RB = new propertyresourcebundle (in );
4. Use the getresourceasstream () method of the class variable
Example: inputstream in = jproperties. Class. getresourceasstream (name );
Properties P = new properties ();
P. Load (in );
5. Use the getresourceasstream () method of Java. Lang. classloader obtained by class. getclassloader.
Example:
Inputstream in = jproperties. Class. getclassloader (). getresourceasstream (name );
Properties P = new properties ();
P. Load (in );
6. Use the getsystemresourceasstream () Static Method of the Java. Lang. classloader class
Example: inputstream in = classloader. getsystemresourceasstream (name );
Properties P = new properties ();
P. Load (in );
7. You can use the getresourceasstream () method of javax. servlet. servletcontext in servlet.
Example: inputstream in = context. getresourceasstream (PATH );
Properties P = new properties ();
P. Load (in );