Home > Java > javaTutorial > body text

How to locate resources in java

coldplay.xixi
Release: 2020-08-17 13:26:14
Original
2349 people have browsed it

Java method of locating resources: 1. Locate resources in the class path through [Class.getResource()]; 2. Locate resources in the root directory of the package through [ClassLoader.getResource()].

How to locate resources in java

[Related learning recommendations: java basic tutorial]

java method of locating resources:

1, through Class.getResource()Locate the resource under the class path (bin/com/bbebfe/icons/sample.gif)

Class clazz = this.getClass();
URL url = clazz.getResource(imageName);
Copy after login

2, through ClassLoader.getResource()Locate the resource in the root directory of the package (bin/icons/sample.gif)

Class clazz = this.getClass();
URLClassLoader loader = (URLClassLoader)clazz.getClassLoader();
URL url = loader.getResource(imageName);
Copy after login

3, provided through ClassLoader.findResource() Locate resources in your own customized way

URL url = loader.findResource(imageName);
Copy after login

So what are the differences between these three methods, and when should we use which method?

1. Class.getResource()Method

This method actually obtains resources through the getResource() method of the Class Loader of the Class. Before calling the getResource() method of the ClassLoader, the Class The .getResource() method will do certain processing on the resource name and construct an absolute name of the resource, which means:

+ If the resource name starts with '/'('/u002f'), then The absolute name of the resource is the part after '/'.

If the imageName is "/icons/sample.gif", it will become "icons/sample.gif"

Otherwise, for other cases, the absolute name will be of the following form (prefix the resource name with modified_package_name/):

          modified_package_name/resource_name (modified package name/resource name)

The corrected meaning of the package name is to replace the '.'('/u002e') in the package name of the current object with '/'

If the ClassLoader.getResource() method returns a value of null URL, the Class.getResource() method will eventually hand over the resource request to ClassLoader.getSystemResource(java.lang.String).

2, ClassLoader.getResource( )Method

This is to search for resources. The name of the resource is a path separated by '/'. This method first searches for its own parent ClassLoader, and then its own parent ClassLoader searches for the resource (actually, If the parent's parent is not null, the parent will still submit a search request upward). If its own parent ClassLoader is null, it will search for the class loaders built into the Java virtual machine and submit resource requests to them. If these operations fail , the ClassLoader will call its own findResource() method to find the resource.

3, ClassLoader.findResource()Method

This method searches for the specified resource internally , if you implement your own Class Loader, you should override this method to find class files and resources in your own specific way.

Related learning recommendations:Programming Video

The above is the detailed content of How to locate resources in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!