您的位置:首页 > 运维架构

(1) 写一个 Properties 格式的配置文件,配置类的完整名称。 * (2) 写一个程序,读取这个 Properties 配置文件,获得类的完整名称并加载这个类,用 反射 的方式运行

2016-04-12 23:02 881 查看
package com.heima.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class Test7 {

/**题目:
* (1) 写一个 Properties 格式的配置文件,配置类的完整名称。
* (2) 写一个程序,读取这个 Properties 配置文件,获得类的完整名称并加载这个类,用
反射 的方式运行 run 方法
* @param args
* @throws IOException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
FileInputStream fis = new FileInputStream("properties");
Properties pp = new Properties();

pp.load(fis);
String str = pp.getProperty("className");
Class clazz = Class.forName(str);
Object obj = clazz.newInstance();
Method me = clazz.getMethod("run", null);
me.invoke(obj, null);

}

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