`
lantian_123
  • 浏览: 1360968 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

getResourceAsStream(String name) name的路径

    博客分类:
  • Java
阅读更多

 

关于class.getResourceAsStream(String name)

class.getClassLoader().getResourceAsStream(String name)的路径name问题

 

首先用类加载资源文件的方式可以有以下三种:

(包结构图)

1)   InputStream inStream = DaoFactory.class.getResourceAsStream("dao.properties");

2)   inStream=DaoFactory.class.getResourceAsStream("/com/jdbc/dao/dao.properties")

3)   inStream=DaoFactory.class.getClassLoader().getResourceAsStream("com/jdbc/dao/dao.properties");

第一种和第二种方式是采用Class对象去加载,第三种采用ClassLoader对象去加载资源文件,之所以Class对象也可以加载资源文件是因为Class类封装的ClassLoadergetResourceAsStream方法。从Class类中的源码中可以看出:

之所以这样做无疑还是方便客户端的调用,省的每次获取ClassLoader才能加载资源文件的麻烦。

路径问题:

1.         第三种是最本质的做法,前两种也是基于第三种去实现的。JVM会使用Bootstrap Loader去加载资源文件。所以路径还是这种相对于工程的根目录即"com/jdbc/dao/dao.properties"(不需要“/)

2.         第一种是采用的相对路径,资源文件相对于当前这个类即(DaoFactory类)的位置,这种方式Class对象做了一些处理。即resloveNamename)这个方法。最终name还是会转换成适合第三方式的name参数

3.         第二种是采用绝对路径,绝对路径是相对于classpath根目录的路径(工程里src目录对应bin目录(存放.class文件的目录))。

Class类中resolveName方法)

  • 大小: 29 KB
  • 大小: 22.9 KB
  • 大小: 10.4 KB
分享到:
评论

相关推荐

    jsp内置对象的用法

    12 InputStream getResourceAsStream(String path) 返回指定资源的输入流 13 RequestDispatcher getRequestDispatcher(String uripath) 返回指定资源的RequestDispatcher对象 14 Servlet getServlet(String ...

    Spring原理模拟代码

    String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]); m....

    数据连接池

    String poolName = name.substring(0, name.lastIndexOf(".")); String url = props.getProperty(poolName + ".url"); if (url == null) { log("没有为连接池" + poolName + "指定URL"); continue; } String ...

    Struts Updownload 源码

    * result中要指定关联关系<param name="inputName">inputStream * */ System.out.println("download action is invoked!!"); String realPath=ServletActionContext.getServletContext().getRealPath("/"+...

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

    Document document = sb.build(this.getClass().getClassLoader().getResourceAsStream(fileName)); Element root = document.getRootElement(); List list = XPath.selectNodes(root, "/beans/bean"); for...

    javaee三大框架整合宅急送项目lib包

    RepositoryService.getResourceAsStream(java.lang.String deploymentId, java.lang.String resourceName) * resourceName 可以通过 deploymentId 动态获得 RepositoryService repositoryService = ...

    jdbc练习的一个工具类

    .getResourceAsStream(DBINFO_FIIE_NAME); try { infos.load(is); } catch (Exception e) { e.printStackTrace(); throw new ExceptionInInitializerError("属性文件加载错误"); } finally { ...

    servlet2.4doc

    The default behavior of this method is to return addHeader(String name, String value) on the wrapped response object. addHeader(String, String) - Method in interface javax.servlet....

    MyBatis3.2.3帮助文档(中文版).zip

    InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);XML 配置文件(configuration XML)中包含了对 ...

    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发送邮件(含附件)的例子

    } /** * 进行用户身份验证时,设置用户名和密码 */ public void setNamePass(String name, String pass) { sendUserName = name; sendUserPass = pass; } /** * 设置邮件主题 * @param mailSubject * @return */ ...

    传智博客jdbc学习案例

    String userDaoClass = prop.getProperty("userDaoClass"); Class clazz = Class.forName(userDaoClass); userDao = (UserDao) clazz.newInstance(); } catch (Throwable e) { throw new ...

    JavaExcel读写库JxlExcel.zip

    模板定义在类路径下新建一个jxl-excel.xml的模板配置文件,输入如下的配置文件内容,即可定义一个excel模板。<?xml version="1.0" encoding="UTF-8" ?>  name="testRead">  <titleRow>  <...

    j2me手机游戏代码

    String[] mapName=new String[]{"牛家村","冰火岛 ","大智岛","襄阳城","蓬莱岛","竞技场"}; boolean updateMan; static Graphics g1; Random rand; Player mapPlayer,attPlayer; boolean isWeather; int ...

Global site tag (gtag.js) - Google Analytics