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

package test;import java.io.FileInputStream;import java.io.InputStream;import java.util.Collection;import java.util.Properties;class Point {int x, y;@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;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 IO流 用类加载器的方式管理资源和配置文件

相关文章:

你感兴趣的文章:

标签云: