您的位置:首页 > 编程语言 > Java开发

Java IO流 用类加载器的方式管理资源和配置文件

2015-05-23 22:39 507 查看
package test;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;

class Point {
int x, y;

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}

public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}

}
//用类加载器
public class Test {
public static void main(String[] args) throws Exception {
//InputStream ips = new FileInputStream("config.properties");

InputStream ips = Point.class.getClassLoader().getResourceAsStream("test/config.properties");

Properties props = new Properties();
props.load(ips);
ips.close();
String className = props.getProperty("className");
Collection collection = (Collection)Class.forName(className).newInstance();

Point pt1 = new Point(1, 1);
Point pt2 = new Point(2, 3);
Point pt3 = new Point(1, 1);
collection.add(pt1);
collection.add(pt2);
collection.add(pt3);

System.out.println(collection.size());
}
}



配置文件:  test/config.properties

配置文件内容:

className=java.util.ArrayList
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java基础