`
elliotann
  • 浏览: 13015 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Java class.getResourceAsStream和ClassLoader.getResourceAsStream区别

 
阅读更多

它们都是去取classpath下的资源,但它们在路径上有一些区别:

        1、Class.getResourceAsStream如果在没有"/"开头的情况下是去相对于此class文件开始进行查询的,如:如果有一个com.elliot.A.class的类文件,如果用A.class.getResourceAsStream("test.xml"),其是去A.class包下即com/elliot/下去查找test.xml文件的。但如果其以A.class.getResourceAsStream("/test.xml"),就是从classpath的绝对路径去查找test.xml文件的,也就是和包com同级下去查找文件。

       2、ClassLoader.getResourceAsStream方式是从classpath的绝对路径开始去查询资源的,其开头不能加"/",其效果和Class.getResourceAsStream以"/"等同。

 

分享到:
评论

相关推荐

    Java 使用getClass().getResourceAsStream()方法获取资源

    主要介绍了Java 使用getClass().getResourceAsStream()方法获取资源的相关资料,这里主要讲解哪种方式可以获取到文件资源,需要的朋友可以参考下

    java-loadresource.rar

    Java加载资源文件的两种方法getResource与getResourceAsStream

    使用J2SE API读取Properties文件的六种方法

    1。使用java.util.Properties类的load()方法  示例: InputStream in = lnew ...InputStream in = JProperties.class.getResourceAsStream(name);  Properties p = new Properties();  p.load(in);

    传智博客jdbc学习案例

    InputStream inStream = DaoFactory.class.getClassLoader() .getResourceAsStream("daoconfig.properties"); prop.load(inStream); String userDaoClass = prop.getProperty("userDaoClass"); ...

    aspose pdf 10.8.0 for java

    刚开始简单破解了一下。 生成PDF都没问题,后来发现生成... InputStream is = XXX.class.getResourceAsStream("/pdflicense.xml"); license.setLicense(is); is.close(); 其中pdflicense.xml已经包含在jar文件里面

    aspose-slides-16.10.0-java.rar

    aspose.slides-16.10.0.jar破解版,亲测可用。...InputStream input = AsposeUtil.class.getResourceAsStream("license.xml"); License license = new com.aspose.slides.License(); license.setLicense(input);

    aspose-words-19.2-jdk16 本地仓库地址

    InputStream is = com.aspose.words.Document.class.getResourceAsStream("/com.aspose.words.lic_2999.xml"); License aposeLic = new License(); aposeLic.setLicense(is); result = true; is.close(); } ...

    aspose-words-19.6-crack.jar

    InputStream is = com.aspose.words.Document.class .getResourceAsStream("/com.aspose.words.lic_2999.xml"); License asposeLicense = new License(); asposeLicense.setLicense(is); is.close(); } catch ...

    dom4j.jar.jar

    InputStream is = Demo1.class.getResourceAsStream("/students.xml");// src目录下 // 1. 获得文档解析器工厂对象 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); // 2. ...

    Java中getResourceAsStream的用法.md

    Java中getResourceAsStream的用法.md

    DruidJDBCUtils.java

    InputStream is = DruidJDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); prop.load(is); ds = DruidDataSourceFactory.createDataSource(prop); } catch (IOException e) { e....

    java读取配置文件的实例代码

    java项目读取配置文件的实例代码,使用class.getClassLoader().getResourceAsStream方法。

    Mybatis源码分析.md

    InputStream inputStream = CommonTest.class.getClassLoader().getResourceAsStream("mybatis-configuration.xml"); SQLSessionFactory SQLSessionFactory = new SQLSessionFactoryBuilder().build(inputStream)...

    Java连接数据库代码

    in = MyTask.class.getClassLoader().getResourceAsStream("db.properties"); prop.load(in); this.sql_ip=prop.getProperty("DB_IP").trim(); this.sql_db_name=prop.getProperty("DB_NAME").trim(); this.sql...

    读取Properties文件的java类

    is = ReadProperties.class.getClassLoader().getResourceAsStream( strFilePath); if (is == null) { WriteLog.writeLog("閰嶇疆鏂囦欢涓嶅瓨鍦紝璇锋鏌ユ枃浠跺悕鎴栬矾寰勬槸鍚︽纭紒"); ...

    pull解析XML

    List<WeatherInfos> infos = weatherService.getWeatherInfos(MainActivity.class.getClassLoader().getResourceAsStream("z.xml")); StringBuffer sb = new StringBuffer(); for(WeatherInfos info : infos){ ...

    DButil.java

    prop.load(DButil.class.getResourceAsStream("/db_config.properties")); info = new DButil(); info.driver = prop.getProperty("driver"); info.dbName = prop.getProperty("dbName"); info.host = prop....

    java解析给定url

    InputStream in = getClass().getResourceAsStream("/config.properties"); try { props.load(in); Enumeration en = props.propertyNames(); System.out.print("读取配置文件:"); while(en....

    java工厂系列设计模式源码与文档

    public class ClassXmlPathApplicationContext implements BeanFactory { private Map, Object> map = new HashMap, Object>(); public ClassXmlPathApplicationContext(String fileName) throws Exception, ...

    jdbc练习的一个工具类

    InputStream is = JdbcUtil.class .getClassLoader() .getResourceAsStream(DBINFO_FIIE_NAME); try { infos.load(is); } catch (Exception e) { e.printStackTrace(); throw new ...

Global site tag (gtag.js) - Google Analytics