Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream

Source: Internet
Author: User

Java path
The paths used in Java are divided into two types: absolute and relative. Specifically, it is divided into four kinds:
An absolute resource path in the form of a URI


such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/aaa.b


A URL is a special case of a URI. The prefix/protocol of the URL must be familiar to Java. A URL can open a resource, and a URI is not.
URLs and Uri objects can be converted to each other, using the respective Touri (), Tourl () method.

Ii. Absolute path of the Local system

D:/java/eclipse32/workspace/jbpmtest3/bin/aaa.b

Java.io the class in the package, you need to use this form of argument.
However, they generally also provide parameters for the type of URI, and the parameters of the URI type accept a URI-style string. Therefore, by using URI conversion, you can also use the absolute path of the URI style in the class in the Java.io package.

Relative path relative to Classpath
such as: relative to
file:/d:/java/eclipse32/workspace/jbpmtest3/bin/the relative path of this path. Among them, bin is the classpath of this project. All Java source file compiled. class files are copied to this directory.

Relative path to current user directory
Is the path that is returned relative to System.getproperty ("User.dir").
For general projects, this is the root path of the project. For Java EE servers, this may be a path to the server. There is no uniform specification for this.
Therefore, never use the relative path to the current user directory. However:
By default, classes in the Java.io package always parse the relative pathname based on the current user directory. This directory is specified by the system Properties User.dir, typically the invocation directory of the Java virtual machine.
This means that it is best not to use a relative path when using a class in the Java.io package. Otherwise, although in the J2SE application may be normal, but in the Java program, there must be problems. And this path is different on different servers.

Relative Path Best Practices
A relative path with respect to the current classpath is recommended
Therefore, when using relative paths, we should use relative paths corresponding to the current classpath.

The ClassLoader class GetResource (string name), getResourceAsStream (string name), and so on, uses relative paths to the classpath of the current project to find resources.

The same is true for the Getbundle (String path) of the ResourceBundle class that is commonly used to read property files.

By looking at the source code for the ClassLoader class and its associated classes, I found that it actually used the absolute path of the URI form. The absolute path of the URI form of the relative path is constructed by obtaining the absolute path of the current Classpath URI form. (This is actually conjecture, because the JDK calls the source code inside the sun, which is not part of the JDK, not open source.) The relative path is essentially an absolute path

So, ultimately, Java can only use absolute paths to find resources in nature. All the relative paths to finding resources are just a few convenient ways. But the API helps us build an absolute path at the bottom to find resources.
Some methods to get classpath and the absolute path of the current class

Here are some ways to get classpath and the absolute path of the current class. You may need to use some of these methods to get the absolute path to the resources you need.

1.filetest.class.getresource ("")
Gets the URI directory of the current class Filetest.class file. does not include himself.

such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/com/test/

2.filetest.class.getresource ("/")
Gets the absolute URI path of the current classpath.

such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/

3.thread.currentthread (). Getcontextclassloader (). GetResource ("")
The absolute URI path of the current classpath is also obtained.

such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/

4.filetest.class.getclassloader (). GetResource ("")
The absolute URI path of the current classpath is also obtained.

such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/

5.classloader.getsystemresource ("")
The absolute URI path of the current classpath is also obtained.

such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/

I recommend using Thread.CurrentThread (). Getcontextclassloader (). GetResource ("") to get the URI representation of the absolute path of the current classpath.

Addressing resources in a Web application
As mentioned earlier, the current user directory, which is the path returned relative to System.getproperty ("User.dir").
For Java EE servers, this may be a path to the server, and there is no uniform specification.
Rather than the root directory of the Web application we published.
In this way, in a Web application, we absolutely cannot use relative paths compared to the current user directory.
In Web applications, we typically get the absolute path of the Web application's root directory through the Servletcontext.getrealpath ("/") method.
In this way, we only need to provide a path relative to the Web application root directory, we can build the absolute path of locating resources.
This is the strategy we generally adopt when developing Web applications.



It is recommended that Thread.CurrentThread (). Getcontextclassloader (). GetResource ("") be used to get the URI representation of the absolute path of the current classpath.

Application can be obtained directly at classes level through the new FileInputStream ("Xx.properties"). The point is that sometimes we need to modify the configuration file through the Web, and we can't write the path dead. After testing feel the following experience:

Read and write in 1.servlet. If the use of struts or servlet can be configured directly in the initialization parameters, the call is based on the servlet's Getrealpath ("/") to obtain the true path, and then according to string file = This.servlet.getInitParameter ("abc"); Gets the relative path of the relative Web-inf.
Cases:
InputStream input =thread.currentthread (). Getcontextclassloader (). getResourceAsStream ("abc.properties");
Properties prop = new properties ();
Prop.load (input);
Input.close ();

Prop.setproperty ("abc", "Test");
Prop.store (new FileOutputStream (path), "–test–");
Out.close ();

2. Directly in the JSP operation, through the JSP built-in object to obtain the absolute operation of the address.
Cases:
JSP page
String path = Pagecontext.getservletcontext (). Getrealpath ("/");
String Realpath = path+ "/web-inf/classes/abc.properties";

Java programs
InputStream in = GetClass (). getClassLoader (). getResourceAsStream ("abc.properties"); Abc.properties placed in the webroot/web-inf/classes/directory
Prop.load (in);
In.close ();

OutputStream out = new FileOutputStream (path); Path for paths passed through the page
Prop.setproperty ("abc", "ABCCCCCC");
Prop.store (out, "–test–");
Out.close ();

3. Manipulate resource files only through Java programs
InputStream in = new FileInputStream ("Abc.properties"); Relative path, path under Project

OutputStream out = new FileOutputStream ("Abc.properties");

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.