this.getClass().getClassLoader().getResource("template");
首先,调用对象的getClass()方法是获得对象当前的类类型,这部分数据存在方法区中,而后在类类型上调用getClassLoader()方法是得到当前类型的类加载器,我们知道在Java中所有的类都是通过加载器加载到虚拟机中的,而且类加载器之间存在父子关系,就是子知道父,父不知道子,这样不同的子加载的类型之间是无法访问的(虽然它们都被放在方法区中),所以在这里通过当前类的加载器来加载资源也就是保证是和类类型同一个加载器加载的。
最后调用了类加载器的getResourceAsStream()方法来加载资源。
===
JAVA运行时,首先会在指定的类路径下(classpath路径下)搜索JAVA编译后的字节码文件(class文件),然后通过类加载器加载到虚拟机中。 DBConn.class.getClassLoader().getResourceAsStream("database.properties") 1、DBConn.class得到表示DBConn类的Class对象,请参照JDK中对Class的说明http://wenku.baidu.com/view/1fa5e8ebe009581b6bd9ebe1.html。 2、通过Class的getClassLoader方法取得加载DBConn类的类加载器对象ClassLoader。 3、调用ClassLoader的getResourceAsStream方法从类加载路径取得文件的输入流(会通过当前的ClassLoader的findResource方法查找指定文件),请参照:http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html#getResourceAsStream%28java.lang.String%29。
===
//下面的JdbcUtils.class后面少了个()吗?应该是都可以
Properties prop = new Properties();
prop.load(JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties"));
String driver = prop.getProperty("driver");
Class.forName(driver);
====
1.[原创]关于ClassName.class.getClassLoader的几点说明.
2.经常希望通过ClassName.class.getClassLoader().getResourceAsStream(““)来取得properties文件.通常:ClassName.class.getClassLoader().getResourceAsStream(““)取得的是WEB-INF的下级目录,比如ClassName.class.getClassLoader().getResourceAsStream(“db.properties“).在Tomcat中,可以通过增加”../”来取得上层目录,即WEB-INF目录,这样就可以把properties放在WEB-INF中统一管理。但是WLS不识别”../”。
3.另外一种土办法,就是不返回classLoader,直接ClassName.class.getResourceAsStream()。然后通过多个”../”(小于6个)来返回相应的上级目录。
4.当然,如果类扩展了HttpServlet,可以通过getServletContext().getRealPath("/")来取得Web部署目录的绝对路径。
====
因此,直接调用 this.getClass().getResourceAsStream(String name);获取流,静态化方法中则使用ClassLoader.getSystemResourceAsStream(String name); 。
====
在JDK中,getResourceAsStream是这样定义的:查找具有给定名称的资源。查找与给定类相关的资源的规则是通过定义类的 class loader 实现的。
因为我是用txt文件当作字典,它存在一个叫dict.aspactword的包下,这样一来,编译后的文件会储存在classPath下,而不是在src下,在动态向字典添加词的时候,classPath下的txt字典没有改变,相对在getResourceAsStream的时候,拿到的自然不是最新的字典,由此感叹,在classPath下如果有不需要改变的文件,在读取时用getResourceAsStream可以保持系统良好的统一性和可移植性,但是在需要改变它们的时候,一定注意修改的路径是classPath下的文件,否则就直接用文件系统的位置来维护你所需要的文件吧
====
常见的有以下两种获取资源文件的方法:
App.class.getClassLoader().getResourceAsStream(String name)
Returns an input stream for reading the specified resource.
The search order is described in the documentation for getResource(String).
默认从classpathe中找文件,name不能带“/”,否则会抛空指针
App.class.getResourceAsStream(String name):
查找资源通过给定名称,查询资源的规则与给定的类的class load来实现,这个方法由类的loader来执行,如果这个类由bootstrap加载,那么方法由ClassLoader.getSystemResourceAsStream代理执行。
代理之前,绝对的资源名称通过传入的name参数以下算法进行构造:
如果name以"/"开头,那么绝对路径是/后边跟的名字
如果name不是以"/"开头,那么绝对路径是package名"."换成“/”以后再加name,例如:
com.abc.App就是/com/abc/App/name
转载:http://xixinfei.iteye.com/blog/1256291
分享到:
相关推荐
主要介绍了Java 使用getClass().getResourceAsStream()方法获取资源的相关资料,这里主要讲解哪种方式可以获取到文件资源,需要的朋友可以参考下
InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml"); try { addressView.setText(MobileInfoService.getMobileAddress(inStream, mobile)); } catch...
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...
主要内容包括 Java类加载机制及加载流程,以及如何定义自己的类加载器,如何实现类的热替换。
Document document = sb.build(this.getClass().getClassLoader().getResourceAsStream(fileName)); Element root = document.getRootElement(); List list = XPath.selectNodes(root, "/beans/bean"); for...
在java类里直接读取配置文件中的信息,用到的方法是this.getClass().getClassLoader().getResourceAsStream
Activiti modeler 设计器汉化文件。引入方式:修改StencilsetRestResource中的InputStream stencilsetStream = this.getClass().getClassLoader().getResourceAsStream("stencilset.json.zh-cn");
hessdroid 是 Resin 的 Hessian 二进制远程调用框架在 Android 上的移植...TestAPI api = (TestAPI) proxyFactory.create(TestAPI.class, webServiceUrl, getClassLoader()); System.out.println(api.saySomething());
List<WeatherInfos> infos = weatherService.getWeatherInfos(MainActivity.class.getClassLoader().getResourceAsStream("z.xml")); StringBuffer sb = new StringBuffer(); for(WeatherInfos info : infos){ ...
Document doc = sb.build(this.getClass().getClassLoader() .getResourceAsStream("beans.xml")); Element root = doc.getRootElement();// 获取根元素 List<?> list = root.getChildren("bean");// 获取...
InputStream inputStream = CommonTest.class.getClassLoader().getResourceAsStream("mybatis-configuration.xml"); SQLSessionFactory SQLSessionFactory = new SQLSessionFactoryBuilder().build(inputStream)...
Icon icon = new ImageIcon(ConfirmDialog.class.getClassLoader().getResource("info.png")); ScaleIcon scaleIcon = new ScaleIcon(icon); JLabel iconLabel = new JLabel(scaleIcon); iconLabel....
InputStream inStream = DaoFactory.class.getClassLoader() .getResourceAsStream("daoconfig.properties"); prop.load(inStream); String userDaoClass = prop.getProperty("userDaoClass"); ...
NULL 博文链接:https://zzc1684.iteye.com/blog/2210510
//System.out.println(ProxyTest.class.getClassLoader().getParent().getClass().getName()); System.out.println(ProxyTest.class.getClassLoader().getClass().getName()); //test1(); //test2(); } ...
librec 这个项目是一个分支,我为自己的研究见解做了一些改进。由Maven管理对于我来说,我更喜欢使用maven和Idea ... InputStream configStream =this . getClass() . getClassLoader() . getResourceAsStream(conf);
is = ReadProperties.class.getClassLoader().getResourceAsStream( strFilePath); if (is == null) { WriteLog.writeLog("閰嶇疆鏂囦欢涓嶅瓨鍦紝璇锋鏌ユ枃浠跺悕鎴栬矾寰勬槸鍚︽纭紒"); ...
InputStream license = ChangePDF.class.getClassLoader().getResourceAsStream("\\license.xml"); try { new License().setLicense(license); } catch (Exception e) { e.printStackTrace(); } Document ...
InputStream inStream = DBCPConnection.class.getClassLoader().getResourceAsStream("dbcp.properties"); propreties.load(inStream); DataSource dataSource = BasicDataSourceFactory.createDataSource...
(1)、request.getRealPath(“/”);//不推荐使用获取工程的根路径 (2)、...//获取工程的根路径,这个方法比较好用,可以直接在servlet和jsp中使用 (4)、 this.getClass().getClassLoader().getResource(“”).getPath();