java类反射读取配置文件
作者:互联网
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
//通过反射运行配置文件
public class ReflectDemo7 {
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//Properties读取文件获取键值对
Properties properties = new Properties();
FileReader fileReader = new FileReader("Example1\\class.txt");
properties.load(fileReader);
fileReader.close();
//获取值
String className = properties.getProperty("className");
String methodName = properties.getProperty("methodName");
//执行方法
Class<?> aClass = Class.forName(className);
Method method = aClass.getMethod(methodName);
method.invoke(aClass);
}
}
标签:aClass,java,读取,配置文件,properties,methodName,import,Properties 来源: https://www.cnblogs.com/codegzy/p/14728364.html