ReflectTest.java package ZHANG.Reflect; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Collection; import java.util.Properties; public class ReflectTest { public static void main(String[] args) { try { //一种方法加载属性文件 //InputStream ips = new FileInputStream("config.properties"); //另一种方法加载属性文件 注意:该文件放到了src目录下 //InputStream ips = ReflectTest.class.getClassLoader().getResourceAsStream("ZHANG/Reflect/config.properties"); InputStream ips = ReflectTest.class.getResourceAsStream("config.properties"); Properties props = new Properties(); props.load(ips); ips.close(); String classname = props.getProperty("CLASSNAME"); Collection conllections = (Collection) Class.forName(classname).newInstance(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { } } } config.properties CLASSNAME = java.util.List;