Java relative path addressing

Source: Internet
Author: User

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 known by 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. This does not have a uniform standard!

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 to the Java program, there will be problems! And this path, in different servers are different!

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.) )

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.