黑马程序员---用类加载器的方式管理资源和配置文件

来源:互联网 发布:三菱plc编程实例精解 编辑:程序博客网 时间:2024/05/21 22:58

---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ---------------------- 

 

我们的配置文件config.properties是放在javaenhance这个项目文件夹下的

我们开发完这个项目就会把这个项目文件夹全部打包发给人家吗?

No!我们没那么大方,我们只将bin那个文件夹下的所有class文件打成 jar 包发给人家。

所以别人并没有拿到你的那个配置文件config.properties

package cn.itcast.day1;import java.io.FileInputStream;import java.io.InputStream;import java.util.Collection;import java.util.HashSet;import java.util.Properties;public class ReflectTest2 {public static void main(String[] args) throws Exception {InputStream ips = new FileInputStream("config.properties");Properties props = new Properties();props.load(ips);ips.close();//自己在被垃圾回收之前先把自己关联的那个系统资源给关闭,要不然这种情况多了以后会发生内存(溢出)泄露。String className = props.getProperty("className");Collection collections = (Collection)Class.forName(className).newInstance();//Collection collections = new HashSet();ReflectPoint pt1 = new ReflectPoint(3,3);ReflectPoint pt2 = new ReflectPoint(5,5);ReflectPoint pt3 = new ReflectPoint(3,3);collections.add(pt1);collections.add(pt2);collections.add(pt3);collections.add(pt1);System.out.println(collections.size());//2,两个元素}}


在实际项目当中几乎没有这么干的,没有用相对路径这么搞的。

你知道这个相对路径是相对谁吗?

实际是这个样子的:

你现在java运行MyClass,用到了xx.file文件。

现在这个xx文件是相对于MyClass还是java.exe?

谁都不是,而是相对于当前工作目录。也就是说xx这个文件一定是在IBM这个目录下。

除了在C盘运行MyClass我们可不可以在D盘运行啊?可以,只要让classpath指向了MyClass就可以。

在任意目录下都可以运行起来这个MyClass,而那个相对文件夹呢,一会儿是相对于IBM,一会儿又是相对于D盘,你说那个配置文件到底放在哪里好?

 接下来几天我们学 javaweb 的时候就会有一个方法 getRealPath() 可以得到项目所在文件夹的绝对路径。

用FileOutputStream的时候,一定要记住用完整的路径,但完整的目录不是硬编码,而是运算出来的。/* getRealPath();  //金山词霸/内部 */

 

现在还有一种方法,是最常用的:

每一个.class文件,我们在用它的时候是不是都要加载到内存里面来?

这个东西叫做“类加载器”。 它可以加载.class文件,那它可不可以加载普通的文件呢?可以

通过本类的Class对象来 get 类加载器,Class 类方法: getClassLoader()

 ClassLoadergetClassLoader()
          返回该类的类加载器。

通过 类加载器 来加载本地资源

java.lang.ClassLoader 类的方法: getResourceAsStream("资源名称")

 InputStreamgetResourceAsStream(String name)
          返回读取指定资源的输入流。

 你放在源文件夹下的内容,除了.java文件,Eclipse会原封不动的 copy 一份到 bin 目录下的 class 目录里。

         

所以这里的路径可以以cn打头:

这种方法只有 InputStream,只能读取资源。

我们未来做的框架的配置文件,都要放在classpath指定的目录下。

发现Class内部也有获取资源的方法:

 InputStreamgetResourceAsStream(String name)
          查找具有给定名称的资源。

 这个原理就是:你每次买可乐都找我,我再去找那个商家。干脆我卖给你可乐得了。

package cn.itcast.day1;public class ReflectPoint {private int x;public int y;private String str1;public String str2;public String str3;public ReflectPoint(int x, int y) {super();this.x = x;this.y = y;}public ReflectPoint(int x, int y, String str1, String str2, String str3) {super();this.x = x;this.y = y;this.str1 = str1;this.str2 = str2;this.str3 = str3;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + x;result = prime * result + y;return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;ReflectPoint other = (ReflectPoint) obj;if (x != other.x)return false;if (y != other.y)return false;return true;}public String toString() {return x + "::" + y + "::" + str1 + "::" + str2 + "::" + str3;}}

 

package cn.itcast.day1;import java.io.FileInputStream;import java.io.InputStream;import java.util.Collection;import java.util.HashSet;import java.util.Properties;public class ReflectTest2 {public static void main(String[] args) throws Exception {//InputStream ips = new FileInputStream("config.properties");//实际开发没有这么搞的,配置文件需要用下面的类加载器加载。//InputStream ips = ReflectTest2.class.getClassLoader().getResourceAsStream("cn/itcast/day1/config.properties");//InputStream ips = ReflectTest2.class.getResourceAsStream("resource/config.properties");//这时是相对于我这个类的路径。InputStream ips = ReflectTest2.class.getResourceAsStream("/cn/itcast/day1/resource/config.properties");//如果写绝对路径的话,从根目录“/”开始。Properties props = new Properties();//Properties是Map的子类,用来存放属性信息(键值对,键和值都是字符串)。props.load(ips);// void load(InputStream inStream) 从输入流中读取属性列表(键和元素对)。 它是个可以将Map集合和IO流关联起来的类。ips.close();//自己在被垃圾回收之前先把自己关联的那个系统资源(配置文件)给关闭,要不刚才那个配置文件一直在内存中占空间,这种情况多了以后会发生内存(溢出)泄露。这个关的不是流这个变量本身,而是关闭系统资源,流这个变量最后在程序运行完后被垃圾回收机制清理。String className = props.getProperty("className");//根据键获取值(获取要创建对象的类的名字)Collection collections = (Collection)Class.forName(className).newInstance();//通过反射创建HashSet集合的对象。//Collection collections = new HashSet();ReflectPoint pt1 = new ReflectPoint(3,3);ReflectPoint pt2 = new ReflectPoint(5,5);ReflectPoint pt3 = new ReflectPoint(3,3);collections.add(pt1);collections.add(pt2);collections.add(pt3);collections.add(pt1);System.out.println(collections.size());//2,两个元素}}

 

---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net

0 0
原创粉丝点击