Java ClassLoader getSystemResourceAsStream()方法与示例

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

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

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

  • getSystemResourceAsStream() method is used to get the resource as a parameter and convert the resource into InputStream or in other words we can say this method is used to represent InputStream for scanning the given resource.

    getSystemResourceAsStream()方法用于获取资源作为参数并将资源转换为InputStream,换句话说,可以说此方法用于表示InputStream来扫描给定资源。

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

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

  • getSystemResourceAsStream() method does not throw an exception at the time of conversion from resource to InputStream object.

    从资源到InputStream对象的转换时, getSystemResourceAsStream()方法不会引发异常。

Syntax:

句法:

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

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

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

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

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

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

Example:

例:

// Java program to demonstrate the example 
// of InputStream getSystemResourceAsStream(String resource_name) 
// method of  ClassLoader 

import java.io.*;

public class GetSystemResourceAsStreamOfClassLoader {
    static String resource(String res) {
        String count = "";
        try {
            Class cl = Class.forName("GetSystemResourceAsStreamOfClassLoader");
            ClassLoader loader = cl.getClassLoader();

            // By using getSystemResourceAsStream() method is to take the 
            //resource and convert it into InputStream
            InputStream is = loader.getSystemResourceAsStream(res);
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            String line;
            while ((line = br.readLine()) != null) {
                count = count + line;
            }
            is.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(resource("getProperties().doc"));
    }
}

Output

输出量

File Writing Done!!


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值