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.