Java ClassLoader getSystemResources()方法与示例

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

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

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

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

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

  • getSystemResources() 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.

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

  • getSystemResources() method may throw an exception at the time of returning URL.

    getSystemResources()方法在返回URL时可能会引发异常。

    IOException: This exception may throw when I/O operation performs.

    IOException:执行I / O操作时可能会引发此异常。

Syntax:

句法:

    public static Enumeration getSystemResources (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 Enumeration, it returns the following values based on the given cases,

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

  • It returns the Enumeration of URL when system resources 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 Enumeration getSystemResources (String resource_name) 
// of ClassLoader method

import java.net.*;
import java.util.*;

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

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

        // It return Enumeration of URL objects with the given //resource name
        Enumeration en = loader.getSystemResources("getProperties().doc");

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

        while (en.hasMoreElements())
            System.out.println(en.nextElement());
    }
}

Output

输出量

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


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

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