Java ClassLoader getSystemResource()方法与示例

ClassLoader类getSystemResource()方法 (ClassLoader Class getSystemResource() method)

  • getSystemResource() method is available in java.lang package.

    getSystemResource()方法在java.lang包中可用。

  • getSystemResource() method is used to find the system resource of the given resource name from the searching location to load classes.

    getSystemResource()方法用于从搜索位置查找给定资源名称的系统资源以加载类。

  • getSystemResource() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get any error.

    getSystemResource()方法是一个静态方法,可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会出现任何错误。

  • getSystemResource() method does not throw an exception at the time of returning URL.

    返回网址时, getSystemResource()方法不会引发异常。

Syntax:

句法:

    public URL getSystemResource (String resource_name);

Parameter(s):

参数:

  • String resource_name – represents the name of the resource.

    字符串resource_name –表示资源的名称。

Return value:

返回值:

The return type of this method is URL, it returns the following values based on the given cases,

此方法的返回类型为URL ,它根据给定的情况返回以下值,

  • It returns the URL when any system resource associated with the given name exists.

    当存在与给定名称关联的任何系统资源时,它将返回URL。

  • It returns null, when no system resource associated with the given name exists.

    当不存在与给定名称关联的系统资源时,它返回null。

Example:

例:

// Java program to demonstrate the example 
// of URL getSystemResource (String resource_name) method 
// of ClassLoader 

import java.net.*;

public class GetSystemResourceOfClass {
    public static void main(String[] args) throws Exception {
        // Get Class by using forName() method
        Class cl = Class.forName("GetSystemResourceOfClass");

        // Get ClassLoader by using ClassLoader
        ClassLoader loader = cl.getClassLoader();

        // It return URL with the given resource name
        URL address = loader.getSystemResource("E://Programs//getProperties.doc");

        // Display address of the resource
        System.out.print("URL of System Resources : ");
        System.out.println(address);
    }
}

Output

输出量

URL of System Resources : file:/E:/Programs/getProperties().doc


翻译自: https://www.includehelp.com/java/classloader-getsystemresource-method-with-example.aspx

你可能感兴趣的:(java,python,jvm,leetcode,spring)