• ClassLoader.getResourceAsStream() 与 Class.getResourceAsStream()的区别


    package com.xinwei.util;
    
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class PropertiesLoader {
        public static void main(String[] args) {
    //        test1();
            test2();
        }
    //在使用Class.getResourceAsStream 时, 资源路径有两种方式, 一种以 / 开头,则这样的路径是指定绝对路径,
    // 如果不以 / 开头, 则路径是相对与这个class所在的包的。
        private static void test2() {
            InputStream in = PropertiesLoader.class.getResourceAsStream("/register.properties");  
            Properties prop = new Properties();  
            try {
                prop.load(in);
                String roleId = prop.getProperty("request.roleIds"); 
                System.out.println(roleId);
            } catch (IOException e) {
                e.printStackTrace();
            }  
            
        }
        //在使用ClassLoader.getResourceAsStream时, 路径直接使用相对于classpath的绝对路径
        private static void test1() {
            InputStream in =PropertiesLoader.class.getClassLoader().getResourceAsStream("register.properties");  
            Properties prop = new Properties();  
            try {
                prop.load(in);
                String roleId = prop.getProperty("request.roleIds"); 
                System.out.println(roleId);
            } catch (IOException e) {
                e.printStackTrace();
            }  
            
        }
    }
    //举例,下面的三个语句,实际结果是一样的:
    //com.explorers.Test.class.getResourceAsStream("abc.jpg")
    //= com.explorers.Test.class.getResourceAsStream("/com/explorers/abc.jpg")
    //= ClassLoader.getResourceAsStream("com/explorers/abc.jpg") 
  • 相关阅读:
    聚类算法学习-kmeans,kmedoids,GMM
    hdu
    高仿精仿微信应用ios源码下载
    UVA 116 Unidirectional TSP 经典dp题
    [置顶] 动态规划之切割钢条
    poj
    求解printf函数?
    实现多文件上传在iOS开发中
    开源DirectShow分析器和解码器: LAV Filter
    <Win32_20>纯c语言版的打飞机游戏出炉了^_^
  • 原文地址:https://www.cnblogs.com/alamps/p/6373654.html
Copyright © 2020-2023  润新知