Java Resources (Resource) get Class.getResource () & Classloader.getresource () __java

Source: Internet
Author: User

Java resources can be a picture, XML files, audio and so on in the form of files, in the development process, especially the use of XML files quite frequently. In Java, class classes and ClassLoader provide a way to locate these resources, such as getresource ().

Resource Name

The complete resource name is the path has absolute and relative points, although they can represent the same resource file, but it does have a certain difference in use. In Java, a path that starts with a backslash (/) is considered an absolute path, whereas a path that begins with a non backslash (/) is called a relative path. The absolute path in our understanding is that there is no forward backslash (/), such as e:/some/directory/path/file.xml, in fact the representation in Java is a forward backslash (/). The real address after the URL for the resource in Java is this:

File:/e:/workspace/test/bin/com/test/main.class

There are also some questions about system and non system resources (abbreviated).

using class &classloader to get resources

Class & ClassLoader need to use the resource name when acquiring resources. When a resource name is passed to class, you can use a forward backslash (/) to indicate that it is an absolute path without using a forward backslash (/) to represent a relative path. When a resource name is passed to a ClassLoader method, the resource file is considered to be loaded as a Java class.

Class

When using class, the name of the resource passed to it can be either an absolute or a relative path. The backslash (/) is typically the root of the compiled class, and the relative path is the path relative to the class that the current caller belongs to.

ClassLoader

when using only ClassLoader, the resource file is considered a class file to load , and the name passed to ClassLoader must conform to the Java specification definition name (Binary name), which is the Java class Package name. class name , if the Java class file as a resource, then the name of the resource is: package name. class name. For example Java.lang.String.class, the resource names in this format are represented from the compiled class root (the file hierarchy of the Java package) and are also applicable to the jar packages on the classpath path. For a resource file in general sense, the Java class file is just a different file suffix.

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.