美文网首页java复习
2020-08-02运行配置文件指定的内容

2020-08-02运行配置文件指定的内容

作者: 智障猿 | 来源:发表于2020-08-12 16:10 被阅读0次
public class Demo {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException, ClassNotFoundException, InstantiationException {
        Properties properties = new Properties();
        FileReader fileReader = new FileReader("test\\test.txt");
        properties.load(fileReader);
        fileReader.close();
        String className = properties.getProperty("className");
        String methodName = properties.getProperty("methodName");
        System.out.println(methodName);
        //通过反射使用
        Class<?> aClass = Class.forName(className);
        //通过反射获得构造方法对象
        Constructor<?> declaredConstructor = aClass.getDeclaredConstructor();
        //通过构造方法对象实例化
        Object o = declaredConstructor.newInstance();
        //通过反射获得方法对象
        Method method = aClass.getMethod(methodName);
        method.invoke(o);
    }
}
捕获.JPG

相关文章

网友评论

    本文标题:2020-08-02运行配置文件指定的内容

    本文链接:https://www.haomeiwen.com/subject/riaprktx.html